I have a single managed document in my entire application (with two entities) that the user is able to update from two different view controllers. Each view controller has its own NSFetchedResultsController property to relay changes to: a UICollectionView in one view controller, and a UITableView in the other. The UICollectionView in the root view controller displays Teams, while the UITableView in the segued view controller displays Players on that team.
When I am in the first view controller, I can add instances of a Team and it works fine as it saves them to the document and then adds them to the UICollectionView. When I am in the second view controller, I make a Player insertion into CoreData and call:
[document saveToURL:document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:nil];
This correctly invokes the NSFetchedResultsControllerDelegate method: -(void)controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
In this delegate method, I call:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
However, calling this row insertion method on the table view causes the following Core Data error:
"CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. * -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0] with userInfo (null)"
My fetchedResultsController certainly is an observer of NSManagedObjectContextObjectsDidChangeNotification, but this error is occurring before any changes in the managed object context. So I am basically unable to determine at which point the managed object context is getting null userInfo.
(Another thing that happens is that my NSFetchedResultsController from the root view controller, which has the UICollectionView, fires its delegate method right before the NSFetchedResultsController in the next view controller, which has the UITableView, fires its own. I think this is correct functionality, as both the Team and the Player are getting updates in CoreData).
Any thoughts on why the UITableView row insertion is causing this error, or how my single managed document could have inconsistencies that might cause this?
In the case of a NSFetchedResultsChangeInsert
event, indexPath
is nil
and newIndexPath
is the destination path for the inserted object. Therefore you have to call
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
for an update event.