I have a collection view that should display a game when I add a game. On the AddGameController
I add a game and save it using core data. When the AddGameVC dismissed, the welcomeVC is displayed. However when inserting a game from AddGameController
, the collection view does not update, unless I kill the app in order to fetched the object when the view load. How could I make it work ?
As a solution, I added the this code collectionView.reloadItems(at: [newIndexPath!])
inside the didChange
method when it's an insertion, and tried to reload the data source before dismissing the AddGameController
it did not make it work.
Update: When there is an insertion I am getting: Thread 1: Exception: "attempt to insert item 0 into section 0, but there are only 0 items in section 0 after the update"
Class AddGameController: UITableViewController {
func textfieldValidation (){
for textfield in textfieldCollection {
if textfield.text!.isEmpty || segmented.selectedSegmentIndex == -1 {
alertMethod()
} else {
self.dismiss(animated: true, completion: nil)
}
}
}
@IBAction func addButtonPressed(_ sender: UIButton) {
save()
textfieldValidation()
}
func save(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let newGame = GameMo(context: appDelegate.persistentContainer.viewContext)
newGame.goal = (Int32(goal.text ?? "0")!)
newGame.rivalGoal = (Int32(rivalGoal.text ?? "0")!)
newGame.shot = (Int32(shots.text ?? "0")!)
newGame.rivalShot = (Int32(rivalsShots.text ?? "0")!)
newGame.nouveau = true
newGame.date = DateManager.dateLong()
appDelegate.saveContext()
}
}
Class WelcomeViewController: UICollectionViewDelegate, UICollectionViewDataSource {
var ops: [BlockOperation] = []
lazy var context : NSManagedObjectContext = {
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}()
lazy var fetchRequestController : NSFetchedResultsController<GameMo> = {
let fetchRequest = NSFetchRequest<GameMo>(entityName: "Game")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
frc.delegate = self
do {
try frc.performFetch()
if let fetchedObjects = frc.fetchedObjects {
print("Fetch Request Activated")
self.gamesMo = fetchedObjects
}
} catch{
fatalError("Failed to fetch entities: \(error)")
}
return frc
}()
override func viewDidLoad() {
super.viewDidLoad()
fetchRequestController.delegate = self
try? fetchRequestController.performFetch()
}
deinit {
for o in ops { o.cancel() }
ops.removeAll()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return gamesMo?.filter{$0.nouveau == true}.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let gameIndex = gamesMo?.filter({$0.nouveau == true})[indexPath.row] {
let userGameScore = gameIndex.goal
let rivalGameScore = gameIndex.rivalGoal
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FormCell", for: indexPath) as? FormCollectionViewCell {
cell.setCell(userScores: Int(userGameScore), rivalScores: Int(rivalGameScore) )
return cell
}
}
return UICollectionViewCell ()
}
}
extension WelcomeViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
print("insert")
print(gamesMo?.count)
ops.append(BlockOperation(block: { [weak self] in
self?.collectionView.insertItems(at: [newIndexPath!])
}))
case .delete:
ops.append(BlockOperation(block: { [weak self] in
self?.collectionView.deleteItems(at: [indexPath!])
}))
case .update:
ops.append(BlockOperation(block: { [weak self] in
self?.collectionView.reloadItems(at: [indexPath!])
}))
case .move:
ops.append(BlockOperation(block: { [weak self] in
self?.collectionView.moveItem(at: indexPath!, to: newIndexPath!)
}))
@unknown default:
break
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
print("TableView endupdates")
collectionView.performBatchUpdates({ () -> Void in
for op: BlockOperation in self.ops { op.start() }
}, completion: {(finished) -> Void in self.ops.removeAll() })
}
}