Everything I\'ve read about using Core Data on multiple threads talks about using multiple NSManagedObjectContext
instances sharing a single NSPersistentS
My own tentative answer to this is now yes.
I initialize my background operation by passing it the NSPersistentStore
instance. On the background thread, the properties of this store, including the URL, are used to create a whole new Core Data stack like this:
// create managed object model
NSURL *modelUrl = [[NSBundle bundleForClass:[self class]] URLForResource:@"..." withExtension:@"..."];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelUrl];
// create persistent store coordinator
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
NSError *error = nil;
[persistentStoreCoordinator addPersistentStoreWithType:[store type]
configuration:[store configurationName]
URL:[store URL]
options:[store options]
error:&error];
// create managed object context
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:persistentStoreCoordinator];
[persistentStoreCoordinator release];
[managedObjectModel release];
I then perform the background fetch using this newly created NSManagedObjectContext
instance.
Everything seems to work just fine. I'm not yet accepting my own answer, though, as I would love to have someone provide supporting or contradicting evidence to my findings.