Search code examples
ioscore-datansfetchedresultscontroller

CoreData error while updating NSManagedObject from NSFetchedResultscontroller with NSPredicate


We have UITableViewController wich DataSource is a NSFetchedResultsController. After clicking on an object in the UITableView you are directed to an UIViewController where you can set a flag (YES/NO) which possibly causes a delete from NSFetchedResultsController (NSPredicate):

  • Objects with flag: YES are shown in UITableView
  • Objects with flag: NO are NOT shown in UITableView

We update the object like following:

objectEntity updatedObject = self.oldobject;
[updatedObject setFlag: [NSNumber numberWithBool: NO]];

NSError *error;
[self.managedObjectContext save:&error];

Saving the updates causes following 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 user Info (null)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException'. reason: '*** -[_NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

Any ideas?

UPDATE:

The exception occurs in the NSFetchedResultsController delegate method

controller()didChangObject()atIndexPath... case: NSFetchedResultsChangeDelete tableView
deleteRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 

*** First throw call stack:
(0x334af2a3 0x3b1cc97f 0x333f934d 0x33407559 0x5e5e5 0x33326fe7 0x33400037 0x33d16599 0x332b4717 0x332b3c77 0x33235bf9 0x332a84cf 0x6174d 0x35478e1b 0x353a20c5 0x353a2077 0x353a2055 0x353a190b 0x353a1e01 0x352ca5f1 0x352b7801 0x352b711b 0x36fcd5a3 0x36fcd1d3 0x33484173 0x33484117 0x33482f99 0x333f5ebd 0x333f5d49 0x36fcc2eb 0x3530b301 0x20abd 0x3b603b20)
libc++abi.dylib: terminate called throwing an exception

Solution

  • In the NSFetchedResultsChangeDelete case of the FRC didChangeObject delegate method the index path of the deleted object is in indexPath, not in newIndexPath. So you should change

    [tableView deleteRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    

    to

    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];