如何创建子NSManagedObjectContext?
我看过一些video/post说可以创建’儿童’MOCs – 使用其他MOC作为持久存储的MOC。 例如,在您正在线程化应用程序的上下文中,并希望拥有一个可以保存/回滚子线程创建的更改的主MOC,这很有用。 (据我所知,一个MOC和它的托管对象必须都在同一个线程上使用)
问题是,我如何创建一个儿童MOC? 我无法追踪我正在观看的WWDCvideo介绍它们,我所看到的一切都在讨论如何使用它们。我可以很容易地分配一个新的MOC,但我该怎么设置它持久存储是另一个MOC?该引用没有显示任何function!
创建一个新的MOC,您可以完全控制同步。 这与调用init
和与父/子关系相同的行为相同。
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
您通过设置其属性将MOC提交给另一个MOC:
moc.parentContext = someOtherMocThatIsNowMyParent;
在这里,孩子选择父母。 我确定我的孩子们希望他们是NSManagedObjectContexts。 父上下文必须是NSPrivateQueueConcurrencyType
或NSMainQueueConcurrencyType
。
您可以创建一个“绑定”到专用队列的MOC:
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
这意味着您只应通过performBlock
或performBlockAndWait
API访问它。 您可以从任何线程调用这些方法,因为它们将确保块中代码的正确序列化。 例如…
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安排它在将来的某个时间,在某个未知线程上异步执行。 方法调用将立即返回。
performBlockAndWait
将与所有其他performBlock
调用进行一些魔术同步,并且当完成此之前已呈现的所有块时,将执行此块。 调用线程将挂起,直到此调用完成。
您也可以创建一个“绑定”到主线程的MOC,就像私有并发一样。
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. }];
这意味着只有当您知道自己位于主线程上时, 或者通过performBlock
或performBlockAndWait
API时才应该直接访问它。
现在,您可以通过performBlock
方法使用“main concurrency”MOC,或者直接知道您已经在主线程中运行。
初始化子MOC然后:
[_childMOC performBlockAndWait:^{ [_childMOC setParentContext:parentMOC]; }];