2

我有一个覆盖 validateForInsert 和 validateForUpdate 的 NSManagedObject 实体。

根据我编写的一些逻辑,当对象一致性出现问题时,此方法会正确返回 NO。

该应用程序是一个由 NSFetchedResultsController 支持的经典 uitableview,带有一个详细视图控制器。

当我添加一个新实体时,详细视图控制器使用 nil objectID 进行实例化,并推入导航堆栈。当我弹出细节控制器时,[context save:&error] 被调用,并且通过放置一些断点,我发现当我添加一个新实体时,获取的委托方法被触发一次,即使 validateForInsert 返回 NO,并且 includePendingChanges 设置为也没有。

当我再次尝试弹出详细信息控制器时,当然会再次调用保存,验证方法也是如此,但是这次 NSFetchedResultsControllerDelegate 没有被触发。

我想知道这是否是正常行为,或者我的模型中是否缺少某些内容。

[更新]

这是委托方法中断点后的堆栈跟踪:

#0  0x0003e5ba in -[MyTableViewController controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:] at ......
#1  0x011512f9 in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] ()
#2  0x00b46a29 in __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_0 ()
#3  0x01759855 in ___CFXNotificationPost_block_invoke_0 ()
#4  0x01759778 in _CFXNotificationPost ()
#5  0x00a8b19a in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#6  0x0106a673 in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] ()
#7  0x01101f5e in -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] ()
#8  0x01065ad3 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] ()
#9  0x0106916b in -[NSManagedObjectContext save:] ()
#10 0x00004490 in -[MyAppDelegate saveContext:]

可以看出,即使在上下文中调用了 save,也没有调用 validate,因此这会使获取的控制器委托被触发。

[更新 2]

特别是,不知道它是否相关,这会导致controllerDidChangeObject中的异常(并非总是如此):

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 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (3), 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). with userInfo (null)

and infact in the origin table has three rows, just because the context bypass the validateFor methods when object is created in context seems that a row is inserted, there is inconsistency in rows number.

The controller delegate are quite simple and standard:

-(void)controllerWillChangeContent:(NSFetchedResultsController*)controller {
   [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
   // for testing purpose I am not doing any modification
   return;
}

-(void)controllerDidChangeContent:(NSFetchedResultsController*)controller {
   [self.tableView endUpdates];
}

Still I don't know who is updating the table view

4

1 回答 1

8

I think this is normal behaviour; the fetched results controller doesn't require a save before it shows changes. If you don't want the changes made in your detail view to appear in the master before they're saved, you'll have to make a separate NSManagedObject context for the detail view. That way, only objects that are saved to the persistent store will make it back to the fetched results controller.

于 2012-08-30T12:42:04.280 回答