合并"NSManagedObjectContextDidSaveNotification"后,NSFetchedResultsController未显示所有结果

Ben*_*hen 4 concurrency core-data nsfetchedresultscontroller ios

我有一个NSFetchedResultsController,它使用谓词获取对象:

isTrash == NO

大部分时间这都按预期工作,但是当一个对象被解除时,获取的结果控制器不会获取未被破坏的对象.

出了什么问题?

Ben*_*hen 20

发生这种情况的原因是由于如何mergeChangesFromContextDidSaveNotification:处理更新的对象.NSManagedObjectContext保留上下文中正在使用的对象的记录,这些对象称为注册对象(NSManagedObjectContext具有访问和有条件地获取已注册对象的方法).mergeChangesFromContextDidSaveNotification:仅处理在上下文中注册的对象的更新.这NSFetchedResultsControllers可以解释问题的原因.

以下是它的表现:

  1. FRC设置的谓词与所有对象都不匹配(从而防止与谓词不匹配的对象在FRC上下文中注册).

  2. 第二个上下文对对象进行更改,这意味着它现在与FRC谓词匹配.第二个上下文被保存.

  3. FRC上下文处理NSManagedObjectContextDidSaveNotification但仅更新其注册对象,因此它不会更新现在与FRC谓词匹配的对象.

  4. 当存在保存时,FRC不执行另一次提取,因此不知道应该包括更新的对象.

修复

解决方案是在合并通知时获取所有更新的对象.这是一个示例合并方法:

-(void)mergeChanges:(NSNotification *)notification {
    dispatch_async(dispatch_get_main_queue, ^{
        NSManagedObjectContext *savedContext = [notification object];
        NSManagedObjectContext *mainContext = self.managedObjectContext;
        BOOL isSelfSave = (savedContext == mainContext);
        BOOL isSamePersistentStore = (savedContext.persistentStoreCoordinator == mainContext.persistentStoreCoordinator);

        if (isSelfSave || !isSamePersistentStore) {
            return;
        }

        [mainContext mergeChangesFromContextDidSaveNotification:notification];

        //BUG FIX: When the notification is merged it only updates objects which are already registered in the context.
        //If the predicate for a NSFetchedResultsController matches an updated object but the object is not registered
        //in the FRC's context then the FRC will fail to include the updated object. The fix is to force all updated
        //objects to be refreshed in the context thus making them available to the FRC.
        //Note that we have to be very careful about which methods we call on the managed objects in the notifications userInfo.
        for (NSManagedObject *unsafeManagedObject in notification.userInfo[NSUpdatedObjectsKey]) {
            //Force the refresh of updated objects which may not have been registered in this context.
            NSManagedObject *manangedObject = [mainContext existingObjectWithID:unsafeManagedObject.objectID error:NULL];
            if (manangedObject != nil) {
                [mainContext refreshObject:manangedObject mergeChanges:YES];
            }
        }                
    });
}
Run Code Online (Sandbox Code Playgroud)