• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - NSManagedObjectContext - 当 parent 改变时如何更新 child ?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:17:28 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

Apple 文档不清楚(或者我找不到)当 parentMOC 插入后保存时在父子 MOC 的情况下会发生什么。

我正在使用 MARCUS ZARRA 的 http://martiancraft.com/blog/2015/03/core-data-stack/方法,顶部有一个 privateQMOC,childMainMOC 作为主线程之一。

问题

我通过在 privateMOC 上调用 save 的后台 Internet 请求将 10,000 个对象添加到 privateMOC,但任何基于 childMainMOC 上下文构建的 NSFetchedResultsController 在父级保存后都不会调用我的委托(delegate)。因此界面不会更新以显示 parentMOC 中的更改。

我想调用一些东西来更新 childMainMOC 中的所有对象 - 然后应该调用子 Controller 上的委托(delegate)方法。

或其他一些解决方案。



Best Answer-推荐答案


好的 - 所以我想通了。

模型如下: 马库斯·扎拉的 http://martiancraft.com/blog/2015/03/core-data-stack/ privateMOC 位于根目录,这允许更好的性能,并且出于其他原因也需要这种方式。我只使用了 2 个 MOC:私有(private)根一个和子一个,它是一个主线程。

然后阅读: 基本上,他解释了 Core Data 如何处理通知等。 http://benedictcohen.co.uk/blog/archives/308

那么 - 我需要做的最后一件事是:确保程序中的所有 objectID 都是真正永久的。

- (id)insertNewObjectForEntityNameNSString *)entityName inManagedObjectContextNSManagedObjectContext *)context {

NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];

// Make sure all inserted objects have a permanent ID.
// THIS IS VITAL. Without getting the permanentIDs, changes that come in from the web will not propogate to the child mainQ MOC and the UI will not update.
// Tested to be performant.
//NSLog(@"testing this - object.objectID is a temp now I think: %@ isTemp:%d", object.objectID, (int) [object.objectID isTemporaryID]);
// http://stackoverflow.com/questions/11990279/core-data-do-child-contexts-ever-get-permanent-objectids-for-newly-inserted-obj
NSError* error = nil;
[context obtainPermanentIDsForObjects[object] error:&error];
if (error || [object.objectID isTemporaryID])
    NSLog(@"obtainPermanentIDsForObjects is NOT WORkING - FIX: a new %@ isTemp: %d !!", entityName, (int) [object.objectID isTemporaryID]);

return object;

}

我还需要按照 Benedicts 的文章 - 监听父根私有(private) MOC 中的更改

/// . http://benedictcohen.co.uk/blog/archives/308  good info !
/// I need this firing as sometimes objects change and the save notification below is not enough to make sure the UI updates.
- (void)privateQueueObjectContextDidChangeNotificationNSNotification *)notification {
    NSManagedObjectContext *changedContext = notification.object;
    NSManagedObjectContext *childContext = self.mainQueueObjectContext;
    BOOL isParentContext = childContext.parentContext == changedContext;
    if (!isParentContext) return;

    //Collect the objectIDs of the objects that changed
    __block NSMutableSet *objectIDs = [NSMutableSet set];
    [changedContext performBlockAndWait:^{
        NSDictionary *userInfo = notification.userInfo;
        for (NSManagedObject *changedObject in userInfo[NSUpdatedObjectsKey]) {
            [objectIDs addObject:changedObject.objectID];
        }
        for (NSManagedObject *changedObject in userInfo[NSInsertedObjectsKey]) {
            [objectIDs addObject:changedObject.objectID];
        }
        for (NSManagedObject *changedObject in userInfo[NSDeletedObjectsKey]) {
            [objectIDs addObject:changedObject.objectID];
        }      
    }];

    //Refresh the changed objects
    [childContext performBlock:^{
        for (NSManagedObjectID *objectID in objectIDs) {
            NSManagedObject *object = [childContext existingObjectWithIDbjectID error:nil];
            if (object) {
                [childContext refreshObjectbject mergeChanges:YES];
                //NSLog(@"refreshing %@", [object description]);
            }
        }
    }];
}
   - (void)privateQueueObjectContextDidSaveNotificationNSNotification *)notification {
    //NSLog(@"private Q MOC has saved");
    [self.mainQueueObjectContext performBlock:^{
        [self.mainQueueObjectContext mergeChangesFromContextDidSaveNotification:notification];
        // I had UI update problems which I fixed with mergeChangesFromContextDidSaveNotification along with obtainPermanentIDsForObjects: in the insertEntity call.
    }];

}

    /// When the MainMOC saves - the user has changed data. 
/// We save up into the privateQ as well at this point. 
- (void)mainQueueObjectContextDidSaveNotificationNSNotification *)notification {
    NSLog(@"main Q MOC has saved - UI level only changes only please");
    [self.privateQueueObjectContext performBlock:^{
        NSError* error = nil;
        if (self.privateQueueObjectContext.hasChanges) {
            [self.privateQueueObjectContext save:&error];
            if (error)
            {
                NSLog(@"Save up error - the actual datastore was not updated : %@", error);
            }
        } else {
            //NSLog(@"No need to save");
        }
    }];
}

关于ios - NSManagedObjectContext - 当 parent 改变时如何更新 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338135/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap