Search code examples
objective-ccore-datansmanagedobjectcontext

Clarifications on NSConfinementConcurrencyType context as child of a parent context


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?


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.