I have the following setup:
NSManagedObjectContext *parent = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSMainQueueConcurrencyType];
// other setup for parent
NSManagedObjectContext *child = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[child setParentContext:parent];
What I want is to save the parent whenever the child saves, so currently I do something like this:
[child performBlock^{
[child save:nil];
[parent performBlock:^{
[parent save:nil];
}
}];
That’s me being safe, and calling save within the context’s own queue. Is that necessary? Could I just do:
[child performBlock^{
[child save:nil];
[parent save:nil];
}];
No, you cannot use the second variant. You would execute the
saveoperation for the parent context on the queue that is associated with the child context, instead of the main queue.This means that the
saveoperation would be executed on a (potentially) different thread than the main thread, which is not allowed because managed object contexts are not thread safe.See also Concurrency Support for Managed Object Contexts
in the Core Data Release Notes for OS X v10.7 and iOS 5.0: