如何创建子NSManagedObjectContext? [英] How do I create a child NSManagedObjectContext?

查看:103
本文介绍了如何创建子NSManagedObjectContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些视频/线程表示可以创建儿童MOC - 使用其他MOC作为其持久存储的MOC。例如,在您正在线程化应用程序的上下文中,并且希望有一个主MOC可以保存/回滚子线程创建的更改。 (从我的理解,MOC和它的managedObjects必须都在同一个线程使用)

I've seen a few videos / threads that say it's possible to create 'children' MOCs -- MOCs that use other MOCs as their persistant stores. Useful, for example, in a context where you're threading your application, and want to have a single master MOC that can save / rollback the changes that the child threads create. (From what I understand, a MOC and it's managedObjects MUST all be used on the same thread)

问题是,如何创建一个孩子MOC?我不能跟踪WWDC的视频,我看到介绍他们,我所看到的一切一直在谈论如何使用他们一旦他们做了我可以很容易地分配一个新的MOC,但如何设置它持久存储是另一个MOC?参考没有显示任何功能!

The question is, how do I create a child MOC? I can't track down the WWDC videos I was watching that introduced them, and everything I"ve seen has been talking about how to use them ONCE they're made. I can easily alloc a new MOC, but how do I set it's persistent store to be another MOC? The reference doesn't show any functions that do that!

推荐答案

创建一个新的MOC你完全控制同步,这和调用 init 一样,和前父/子关系一样。

Create a new MOC for which you are in total control of the synchronization. This is the same as calling init and the same behavior as pre-parent/child relationships.

NSManagedObjectContext *moc = [[NSManagedObjectContext alloc]
       initWithConcurrencyType:NSConfinementConcurrencyType];

您通过设置MOC的属性,将MOC作为父级MOC:

You parent a MOC to another MOC by setting its property:

moc.parentContext = someOtherMocThatIsNowMyParent;

这里,孩子选择父母。 NSManagedObjectContexts。父上下文必须是 NSPrivateQueueConcurrencyType NSMainQueueConcurrencyType

Here, the child chooses the parent. I'm sure my kids wish they were NSManagedObjectContexts. A parent context must be of either NSPrivateQueueConcurrencyType or NSMainQueueConcurrencyType.

您可以创建一个绑定到私有队列的MOC:

You can create a MOC that is "bound" to a private queue:

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

这意味着您只能通过 performBlock performBlockAndWait API。你可以从任何线程调用这些方法,因为它们将确保块中的代码的正确序列化。例如...

which means you should only access it via the performBlock or performBlockAndWait API. You can call those methods from any thread as they will ensure proper serialization of the code in the block. For example...

NSManagedObjectContext *moc = [[NSManagedObjectContext alloc]
       initWithConcurrencyType:NSPrivateQueueConcurrencyType];
moc.parentContext = someOtherMocThatIsNowMyParent;
[moc performBlock:^{
    // All code running in this block will be automatically serialized
    // with respect to all other performBlock or performBlockAndWait
    // calls for this same MOC.
    // Access "moc" to your heart's content inside these blocks of code.
}];

performBlock performBlockAndWait performBlock 将创建一个代码块,并调度它与Grand Central Dispatch在一段时间异步执行未来,在一些未知的线程。方法调用将立即返回。

The difference between performBlock and performBlockAndWait is that performBlock will create a block of code, and schedule it with Grand Central Dispatch to be executed asynchronously at some time in the future, on some unknown thread. The method call will return immediately.

performBlockAndWait 会与所有其他 performBlock 调用,并且当在此之前呈现的所有块都完成时,该块将执行。

performBlockAndWait will do some magic synchronization with all the other performBlock calls, and when all the blocks that have been presented prior to this one are done, this block will execute. The calling thread will pend until this call has completed.

您也可以创建一个绑定到主线程的MOC,就像私有并发性一样。

You can also create a MOC that is "bound" to the main thread, just like private concurrency.

NSManagedObjectContext *moc = [[NSManagedObjectContext alloc]
       initWithConcurrencyType:NSMainQueueConcurrencyType];
moc.parentContext = someOtherMocThatIsNowMyParent;
[moc performBlock:^{
    // All code running in this block will be automatically serialized
    // with respect to all other performBlock or performBlockAndWait
    // calls for this same MOC.  Furthermore, it will be serialized with
    // respect to the main thread as well, so this code will run in the
    // main thread -- which is important if you want to do UI work or other
    // stuff that requires the main thread.
}];

这意味着你应该只能直接访问它,如果你知道你在主线程,或$ <$ c> performBlock 或 performBlockAndWait API。

which means you should only access it directly if you know you are on the main thread, or via the performBlock or performBlockAndWait API.

现在,您可以通过 performBlock 方法使用主并发MOC,或直接使用如果,您知道您已经在运行主线程。

Now, you can use the "main concurrency" MOC either via the performBlock methods, or directly if you know you are already running in the main thread.

这篇关于如何创建子NSManagedObjectContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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