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)
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!
Initialize child MOC then:
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.You parent a MOC to another MOC by setting its property:
Here, the child chooses the parent. I'm sure my kids wish they were NSManagedObjectContexts. A parent context must be of either
NSPrivateQueueConcurrencyType
orNSMainQueueConcurrencyType
.You can create a MOC that is "bound" to a private queue:
which means you should only access it via the
performBlock
orperformBlockAndWait
API. You can call those methods from any thread as they will ensure proper serialization of the code in the block. For example...The difference between
performBlock
andperformBlockAndWait
is thatperformBlock
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
will do some magic synchronization with all the otherperformBlock
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.You can also create a MOC that is "bound" to the main thread, just like private concurrency.
which means you should only access it directly if you know you are on the main thread, or via the
performBlock
orperformBlockAndWait
API.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.