【问题标题】:NSManagedObjectContext async import, save and notify main contextNSManagedObjectContext 异步导入、保存和通知主上下文
【发布时间】:2013-09-29 11:26:15
【问题描述】:

我的父子上下文如下: 1. writercontext 与NSPrivateQueueConcurrencyType 2. mainContext 与NSMainQueueConcurrencyType ParentContext:writercontext 3. 和背景上下文NSPrivateQueueConcurrencyTypeParentContext:writercontext

如何通过后台上下文所做的更改通知主上下文?

我已阅读 the last part: async save,但它不会在后台保存或导入,它会导致 UI 被阻止且无响应。有没有办法在背景中使用子父上下文并仍然通知主上下文?

目前我保存我的上下文:

[context performBlockAndWait:^{
    @try {
        NSError *childError = nil;
        if ([context save:&childError])
        {
            [context.parentContext performBlockAndWait:^{
                NSError *parentError = nil;
                if ([context.parentContext save:&parentError])
                {
                     //saved
                }
                else
                {
                    nslog(@"Error: %@", parentError.description);
                }
            }];
        }
        else
        {
            DBERROR(@"Error: %@", childError.description);
        }
    }
    @catch (NSException *exception)
    {
        DBERROR(@"Exception: %@", exception.description);
    }

}];

【问题讨论】:

  • 有什么想法可以解决这个问题吗?

标签: iphone core-data nsmanagedobjectcontext


【解决方案1】:

我假设context 是您的背景上下文。 如果你从主线程调用performBlockAndWait,它将被阻塞直到阻塞完成。您应该将代码替换为:

[context performBlock:^{

...

}];

这样主线程不会被阻塞,因为阻塞会在另一个线程上执行。

至于保存,我猜您的更改不会传播到您的mainContext。我自己没有使用过嵌套上下文,所以我不确定它为什么会这样工作(也许您需要手动合并上下文之间的更改)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    相关资源
    最近更新 更多