使用NSPredicate从NSFetchedResultscontroller更新NSManagedObject时出现CoreData错误

时间:2013-01-30 11:39:05

标签: ios core-data nsfetchedresultscontroller

我们有UITableViewController,DataSource是NSFetchedResultsController。单击UITableView中的对象后,您将被定向到UIViewController,您可以在其中设置一个标志(是/否),可能导致从NSFetchedResultsController(NSPredicate)删除:

  • 带有标志的对象:YES显示在UITableView
  • 带有标志的对象:NO未显示在UITableView

我们更新对象如下:

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

NSError *error;
[self.managedObjectContext save:&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]'

有什么想法吗?

更新

NSFetchedResultsController委托方法

中发生异常
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

1 个答案:

答案 0 :(得分:3)

在FRC NSFetchedResultsChangeDelete委托方法的didChangeObject情况下,已删除对象的索引路径位于indexPath,而不是newIndexPath。所以你应该改变

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

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