Can I set the parent context of my ManagedObjectContext to a ManagedObjectContext with a different concurrency type? For example:
backgroundManagedObjectContext_ = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[backgroundManagedObjectContext_ setPersistentStoreCoordinator:coordinator];
managedObjectContext_ = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[managedObjectContext_ setParentContext:backgroundManagedObjectContext_];
My goal is to (hopefully) get a fast save for managedObjectContext_ (as it only needs to save things to the parent in-memory context) and then have the backgroundManagedObjectContext_ do a save on its own queue. Unless I happen to need to do another save from the "foreground" queue before the background one saves my user should never see long save times.
I'm running into what look like deadlocks, but I'm not sure if they are related to this, or if my problem is elsewhere.
Details for the one place where I can more or less reliably produce the deadlock:
I have a managed object context for the main thread, it is backed by a managed object context with a private queue concurrency type, and that one has a persistent store.
I have a handful of entity types (about 5), each with one or two bi-directional relationships to another entity.
My app wants to use iCloud (I have that code dialed for these tests), so it can't ship with a pre-populated database, it needs to build it when it detects an empty one.
So when I see an empty database (this is checked on the main thread) I add around 4 or 8 of all but one of the entity types, and that last one gets about 100 (all of the adds happen on the main thread). The main thread does a saveContext. After that completes (with no errors) it asks the other managed object context to run a block that also does a saveContext. This saveContext is definitely a deadlock participant. This is also the ONLY stuff done with the "background" NSManagedObjectContext at all.
The exact control flow is a little murky after this, as a NSFetchedResultsController (all of a given entity type (which has ~3 members) with a simple sort, and a batch size of 20 or so) drive the next round of Core Data activity, but there is call from the TableViewController asking how many items it needs to manage, which is "how many results does the fetched results controller have". That call is the other side of the deadlock. (all this is in the main thread)
childContext
/parentContext
orchild
/parent
. – Alsatian