Is initializing a NSManagedObjectContext
using NSMainQueueConcurrencyType
only for the situation where that MOC has a child MOC that was initialize
NSMainQueueConcurrencyType
is mainly for the contexts linked to the UI.
To keep the UI responsive, most of the business logic is best done in background threads and in "background" contexts. But the UI itself will need to use a context at some point, i.e. NSMainQueueConcurrencyType
.
Child contexts with NSMainQueueConcurrencyType
are well suited to editing panes where the changes can be saved at once, ie committed to the parent context. The parent does not need to use NSMainQueueConcurrencyType
.
NSConfinementConcurrencyType
is the default type. It links the context to the current thread, pretty often the main thread by the way. You shouldn't rely on the default type but in the simplest applications. NSMainQueueConcurrencyType
and NSPrivateQueueConcurrencyType
are best since you precisely know which queue is in use for every context.
First a recipe on Core Data new context types.
NSMainQueueConcurrencyType
creates a context that is associated with the main dispatch queue and thus the main thread. You could use such a context to link it to objects that are required to run on the main thread, for example UI elements.
NSPrivateQueueConcurrencyType
creates and manages a private dispatch queue to operate on. You must use the new methods performBlock:
or performBlockAndWait:
. The context will then execute the passed blocks on its own private queue.
Finally, NSConfinementConcurrencyType
is the default type and can be used only within the thread where it has been created. So, within your NSOperation
, you have used it in the right manner. A simple note. If you want to use it as a child context, you need to have a "queue context" (NSMainQueueConcurrencyType
or NSPrivateQueueConcurrencyType
).
Now, about your question.
Is initializing a NSManagedObjectContext using NSMainQueueConcurrencyType only for the situation where that MOC has a child MOC that was initialized using NSPrivateQueueConcurrencyType?
No, not necessary. Yes, you could set up a private context that does some work in background and then pushes the retrieved objects to the main one, but I will do the contrary: use a NSPrivateQueueConcurrencyType
as the master context and the NSMainQueueConcurrencyType
as a child context for the former. In this way, the main context will deal only with objects that are in memory. Save to disk are performed by the private queue only.
This approach is used by the UIManagedDocument
class. Save to disk are performed in a background thread (a private queue). In this manner the UI is not freezed.