0

My problem is that I have two tableViews in two different view Controllers and when I update the data in one, the other doesn't update the tableView.

FetchedResultsController (identical in both VC's)

fileprivate lazy var fetchedResultsControllerGift: NSFetchedResultsController<GiftMO> = {
    // Create Fetch
    let request: NSFetchRequest<GiftMO> = GiftMO.fetchRequest()
    // Configure
    let sectionDescriptor = NSSortDescriptor(key: "recName", ascending: true)
    let timeDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)
    request.sortDescriptors = [sectionDescriptor, timeDescriptor]
    // Create Controller
    let fetchedResultsControllerGift = NSFetchedResultsController(fetchRequest: request, managedObjectContext: DatabaseController.createMoc(), sectionNameKeyPath: "recName", cacheName: nil)
    // Configure Controller
    fetchedResultsControllerGift.delegate = self
    return fetchedResultsControllerGift
}()

I can confirm that the FRC's methods are fired in the second viewController (controller will/did change content) and the data is correct, the tableView just fails to reflect the changes. I've tried placing self.occTableView.reloadData() in the second VC's viewWillAppear but this just gives the error :

"An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. attempt to delete row 2 from section 2, but there are only 1 sections before the update with userInfo (null)"

My viewWillAppear in second VC:

super.viewWillAppear(animated)
    do {
        try self.fetchedResultsControllerGift.performFetch()
    } catch {
        print("Error: \(error)")
    }
    self.occTableView.reloadData()

PROBLEM SOLVED For anyone who also runs into this. If you have any kind of collectionView or tableView embedded in ANOTHER repeating view ( I had a tableView embedded in a collectionViewCell ) The parent viewController cannot be the embedded view's delegate or datasource. You must do ALL of the view's configuring (rows, sections, etc.) inside the class of the CELL of the parent View. I had to program all of my core data (FRC) and tableView configuration inside my collectionViewCell's class and it works perfect! Oh and make sure to also link an @IBOutlet for the embedded view to the CELL of its parent view.

SEE: https://stackoverflow.com/a/42628971/7277906

Thanks so much for helping!!

Cœur
  • 37,241
  • 25
  • 195
  • 267
M. Doe
  • 1
  • 2
  • Are you implemented the fetched results controller delegate protocol methods? – Mannopson Mar 29 '17 at 01:04
  • Hello Yes!! I have all of the ControllerWill/Did Change Content and everything in between. I also have the delete method in TableView Data Source. Will update question with the code. – M. Doe Mar 29 '17 at 01:28
  • Could it be an issue with both FRC's pointing to the same MOC and both of them assigning themselves as the delegate? I've tried just about everything and can't seem to move past this. – M. Doe Mar 29 '17 at 01:38
  • Looks everything is good! Are you implemented table view data source's titleForHeaderInSection method? – Mannopson Mar 29 '17 at 02:20
  • Yes, both VC's use identical versions of that method. Only the first VC Tableview updates correctly though... – M. Doe Mar 29 '17 at 02:25
  • NOTE: If i make changes to the data, they will be reflected in the second VC only if it is the first time the view is loaded, also when the app restarts, the changes are reflected. They just aren't updated while the app is running. – M. Doe Mar 29 '17 at 02:27
  • Try to call tableView.reloadData() when the delete operations is completed. – Mannopson Mar 29 '17 at 02:31
  • And remove the fileprivate prefix – Mannopson Mar 29 '17 at 02:32
  • Did both suggestions, still same result. I may have realized the problem though. The TableView that is having the issue, is embedded in a UICollectionViewCell. Could that mess up the tableView's datasource/delegate? Currently the tableViews datasource/delegate are just set to the ViewController. – M. Doe Mar 29 '17 at 02:41

0 Answers0