You certainly got this error:
CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of sections. The number of sections contained in the table view after the update (8) must be equal to the number of sections contained in the table view before the update (8), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted). with userInfo (null)
This message just doesn't make sense to me as CoreData is related to NSFetchedResultsController BUT CoreData is not related to the table view. (Correct me if I'm wrong) The way I understand it, the only link between tableview and NSFetchedResultsController is in NSFetchedResultsController's delegate, when we use controller(didChangeObject:) and controller(didChangeSection:)
How is it possible that the iOS framework knows about the tableView number of sections when it's not even mandatory (It's recommended but not mandatory) to use a UITableView... It should only be able to check the NSFetchedResultsController result fetchedObjects result array and not in the tableview.
Typically, the controller's delegate is implemented in a following way (taken from Apple's Core Data Programming Guide):
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[[self tableView] endUpdates];
}
When the tableView
gets endUpdates
message, it does two things that are important in our context:
Counts how many sections were inserted and / or deleted since beginUpdates
(by using insertSections:withRowAnimation
method)
Checks if these changes were reflected in the data source. In your case, they were not: there was 1 section inserted by calling insertSections:withRowAnimation
on the table view, but the data source still returns 8 when asked about section count. That's basically what the error message is saying.
So the table view figures out that its state is inconsistent and it throws and exception.
And we're still in the controllerDidChangeContent
delegate method. Let's take a look on the error message again:
An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent
So NSFetchedResultsController
calls controllerDidChangeContent
on its delegate inside a try
block and catches the exception. Then it re-throws it.
Long story short: NSFetchedResultsController
knows nothing about the table view - it just caught the exception which happened to be thrown in endUpdates
method in UITableView
.