0

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() })
        }
    
}
Vangola
  • 128
  • 7

1 Answers1

0

You could simply do the performFetch in viewDidAppear of WelcomeViewController to fix this issue.

class WelcomeViewController: UIViewController {
    //...
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        fetchRequestController.delegate = self
        try? fetchRequestController.performFetch()
    }
}

Modify save() to save core data context using saveContext.

func save(){
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    //...
    appDelegate.saveContext()
}
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • @Vangola Maybe the save is not working in your custom save method. Try adding `appDelegate.save()` in you custom `save()` method. – Frankenstein Jul 28 '20 at 17:06
  • The save method is working, since I can see all added items when I restart the app with the `performFetch` from the` viewDidLoad`. – Vangola Jul 28 '20 at 17:09
  • There is a save in `applicationwillterminate` method. This would be the reason for save to work on relaunch. – Frankenstein Jul 28 '20 at 17:10
  • I updated the code. But in my Xcode, I have the `appDelegate.saveContext()` inside the save method. And I can see a game when I press the save button from DB Browser for SQL lite. `appDelegate.saveContext() Is not the problem. – Vangola Jul 28 '20 at 17:18
  • If saving wasn't the problem then by fetching in `viewDidAppear` should have fixed the issue. Is there anything that you're missing to add here? – Frankenstein Jul 28 '20 at 22:19