在私有/后台队列上创建NSManagedObjectContext:怎么办? [英] Creating a NSManagedObjectContext on a private/background queue: how to do?

查看:217
本文介绍了在私有/后台队列上创建NSManagedObjectContext:怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑如何在主线程以外的其他线程上创建 MOC



文档中,可以读


这样做的后果是上下文假定默认所有者是
分配它的线程或队列 - 这是由调用其init方法的线程
决定的。因此,您不应该在一个线程上初始化
上下文,然后将其传递给不同的线程。


另一方面,我已经看到代码,其中辅助 MOC 是通过以下方式创建在主线程

  NSManagedObjectContext * parentContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; 

[parentContext performBlockAndWait:^ {
[parentContext setUndoManager:nil]; // no point in it supporting undo
[parentContext setPersistentStoreCoordinator:coordinator];
}];

那么,什么是创建辅助 MOC ?也许我应该准确地说,这个辅助 MOC 是项目的中心类的 @property AppDelegate )。

解决方案

文档最多含糊不清。



在我的测试中,您不需要使用队列将 NSManagedObjectContext 附加到其父级或 NSPersistentStoreCoordinator



如果你正在做一个 NSConfinmentConcurrencyType



如果你正在做一个 NSPrivateQueueConcurrencyType

/ code>我在创建它的线程上设置父或协调器。它是限制或限制到私有队列的 NSManagedObjectContext 的用法。我定义的用法是执行抓取,删除对象等。配置 NSManagedObjectContext 不受限制。



这当然不在文档中,但是当访问 NSManagedObjectContext 错误地引起异常时,通过测试返回结果。


I am confused about how to create a MOC on other threads than the main thread.

On one hand, in the doc, one can read

A consequence of this is that a context assumes the default owner is the thread or queue that allocated it—this is determined by the thread that calls its init method. You should not, therefore, initialize a context on one thread then pass it to a different thread.

But on the other hand, I have seen code where an auxiliary MOC is created the following way, on the main thread:

    NSManagedObjectContext *parentContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];

    [parentContext performBlockAndWait:^{
        [parentContext setUndoManager:nil]; // no point in it supporting undo
        [parentContext setPersistentStoreCoordinator:coordinator];
    }];

So, what is the good way to create an auxiliary MOC? Maybe I should precise that this auxiliary MOC is a @property of a central class of the project (AppDelegate for example).

解决方案

The docs are ambiguous at best.

In my testing, you do not need to use a queue to attach the NSManagedObjectContext to its parent or the NSPersistentStoreCoordinator.

If you are doing a NSConfinmentConcurrencyType I set the parent or coordinator on the thread that created it (since that is the thread that can use it).

If you are doing a NSPrivateQueueConcurrencyType I set the parent or coordinator on the thread that created it as well. It is the usage of the NSManagedObjectContext that is confined or restricted to the private queue. I define the usage as executing a fetch, deleting an object, etc. Configuring the NSManagedObjectContext is not restricted.

This of course is not in the documentation but the results were produced through testing back when accessing a NSManagedObjectContext incorrectly caused an exception.

这篇关于在私有/后台队列上创建NSManagedObjectContext:怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆