I’m slowly migrating to iOS 5 Core Data new APIs and I’m investigating about NSConfinementConcurrencyType.
From Apple documentation
Confinement (NSConfinementConcurrencyType). This is the default. You
promise that context will not be used by any thread other than the one
on which you created it. (This is exactly the same threading
requirement that you’ve used in previous releases.)
The thing is not really obvious to me is the meaning of default in this context. What it does really mean?
Usually, if I need to perform a really long import operation in CD, I set up a new NSOperation class and the I create its own context.
- (void)main
{
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
// other code here...
}
Now, I can take advantage of parent context and do the following:
- (void)main
{
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
[moc setParentContext:[self masterMoc]];
// other code here...
}
Said this, what about NSConfinementConcurrencyType? What could be a typical usage example?
By "default," Apple means that this is the standard way Core Data MOCs have always worked. The MOC must be created on the thread upon which it is used. In your code above, you are following the standard historically effective MOC usage pattern. The other styles of MOCs create and manage their own background queues or bind to the main queue. This can simplify things somewhat. Or not. Primarily, they are block oriented interfaces to the MOC. Sometimes that is nice to have. (While blocks are the new hotness, they are not an unalloyed good. I have seen block oriented code that is written by lazy programmers. They leave way too much complexity in their methods. Maintenance of such code can be quite hard with subtle interactions.)
The idea of a parent context is orthogonal to that of thread confinement. The parent context is one way to quickly associate most of the parameters needed by a MOC. (Apple is unclear what is actually carried across between the MOCs. Nor do they discuss merge policy issues.) You still have to catch, I believe, the inter-MOC notifications.
If I may suggest, don’t over think these things. The changes to Core Data for Lion/iOSv5 were quite modest.