xcode 如何创建子 NSManagedObjectContext?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12271464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a child NSManagedObjectContext?
提问by RonLugge
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——使用其他 MOC 作为其持久存储的 MOC。例如,在您对应用程序进行线程处理并希望拥有一个可以保存/回滚子线程创建的更改的单个主 MOC 的上下文中很有用。(据我所知,MOC 和它的 managedObjects 都必须在同一个线程上使用)
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?我无法找到我正在观看的介绍它们的 WWDC 视频,而且我所看到的一切都在讨论一旦制作完成后如何使用它们。我可以轻松分配一个新的 MOC,但是我该如何设置它持久存储是另一个 MOC?参考没有显示任何这样做的函数!
回答by Jody Hagins
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.
创建一个您可以完全控制同步的新 MOC。这与呼叫init
相同,行为与前父/子关系相同。
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSConfinementConcurrencyType];
You parent a MOC to another MOC by setting its property:
您可以通过设置 MOC 的属性将其作为另一个 MOC 的父级:
moc.parentContext = someOtherMocThatIsNowMyParent;
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
.
在这里,孩子选择父母。我确定我的孩子希望他们是 NSManagedObjectContexts。父上下文必须是NSPrivateQueueConcurrencyType
或NSMainQueueConcurrencyType
。
You can create a MOC that is "bound" to a private queue:
您可以创建一个“绑定”到专用队列的 MOC:
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSPrivateQueueConcurrencyType];
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...
这意味着您只能通过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.
}];
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.
performBlock
和之间的区别在于performBlockAndWait
,performBlock
它将创建一个代码块,并使用 Grand Central Dispatch 调度它以在未来某个时间在某个未知线程上异步执行。方法调用将立即返回。
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.
performBlockAndWait
将与所有其他performBlock
调用进行一些神奇的同步,并且当在此之前呈现的所有块都完成时,将执行此块。调用线程将挂起,直到此调用完成。
You can also create a MOC that is "bound" to the main thread, just like private concurrency.
您还可以创建一个“绑定”到主线程的 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.
}];
which means you should only access it directly if you know you are on the main thread, orvia the performBlock
or performBlockAndWait
API.
这意味着只有在知道自己在主线程上或通过performBlock
或performBlockAndWait
API时才应该直接访问它。
Now, you can use the "main concurrency" MOC either via the performBlock
methods, or directly ifyou know you are already running in the main thread.
现在,您可以通过performBlock
方法使用“主并发”MOC ,或者如果您知道自己已经在主线程中运行,则可以直接使用。
回答by mkral
Initialize child MOC then:
然后初始化子 MOC:
[_childMOC performBlockAndWait:^{
[_childMOC setParentContext:parentMOC];
}];