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!!