Question

I have a storyboard that looks like this:

Summary List -> Recursive Table View -> Item View

It is managed by a navigation controller and for the best part works fine and as I'd expect. I can edit data without issue in the summary list:

Summary List -> Recursive Table View -> Item View
  |
  | <- (make a change here, use beginUpdate and endUpdate)
  |
Core Data

This bit works fine...

What should happen is that when one gets to the Item View the core data objects are updated and Item View will then appear on the next load of the summary list.

However, for some reason Summary List is still being refreshed in the background, I assume because it's in the navigation hierarchy, and therefore when I update the CoreData objects at the Item View board I get the classic 'CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent' - normally I would address this by ensuring I'd called beginUpdate and endUpdate but how do I do that if there might be a number of tableviews between me and the tableView in question?

This bit does not work:

Summary List -> Recursive Table View -> Item View
  |                                         |
  |                                         | > (a change here modifies the core
  |                                              data database, Summary List is
  |                                              still somehow loaded and error)
  |
Core Data

The full error is the classic 'you've done it wrong':

CoreData: error: Serious application error.  An exception was
caught from the delegate of NSFetchedResultsController during a call
to -controllerDidChangeContent:
Invalid update: invalid number of rows in section 1.
The number of rows contained in an existing section after the update
(0) must be equal to the number of rows contained in that section
before the update (1), plus or minus the number of rows inserted or 
deleted from that section (0 inserted, 0 deleted) and plus or minus
the number of rows moved into or out of that section (0 moved in, 0 moved out).
Was it helpful?

Solution

If you've set the tables up with a fetched results controller in the standard manner, I'm pretty sure it doesn't matter if the table is on screen or not. It sounds like some unusual updates are being done and/or you're not using the fetched results controller as a datasource properly.

If you're having problems, you can turn off tracking by setting the fetched results controller's delegate to nil. Try this when the view goes offscreen, and restore (and reload the table) when it comes back.

Note that the exception you're seeing isn't due to missing begin and endUpdates calls. If they were missing, you wouldn't get the exception. They are happening, but inside there you don't have a match between the inserts/deletes and final row count. Before the beginUpdates you have a row in section 1, after you don't, and during you don't delete anything.

OTHER TIPS

You need to implement your fetched results controller delegate methods. In the delegate callbacks, make sure you delete and add the appropriate number of cells and sections. Then this error will surely vanish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top