Question

While reading the doc about NSConfinementConcurrencyType, I read the following

You cannot use this concurrency type in conjunction with the new nested contexts feature

This sentence is quite weird to me since I can do, for example, within an NSOperation subclass like

NSManagedObjectContext * localMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
[localMOC setParentContext:[self mainContext]];

where the mainContext can be injected from outside and is of type NSMainQueueConcurrencyType.

Am I missing something?

Was it helpful?

Solution

Yes, that documentation is poorly worded.

What I think it means is that you cannot use:

NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
[moc setParentContext:[self managedObjectContext]];

Which I have tested previously and it did fail with an error. YMMV of course as I tested it in iOS 5.x.

That is different than:

NSManagedObjectContext * localMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
[localMOC setParentContext:[self mainContext]];

Which works just fine and is part of the newer Core Data design.

There is a subtle difference between the two initializers and the older, historic, initializer is missing some key component to allow parent-child contexts to work properly.

OTHER TIPS

I might be wrong, but according to a short test is seems to me that the restriction on NSConfinementConcurrencyType

You cannot use this concurrency type in conjunction with the new nested contexts feature

applies to the parent context and not to the child context. So

NSManagedObjectContext * localMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
[localMOC setParentContext:[self mainContext]];

works, as long as [self mainContext] has been created with the main or private queue concurrency type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top