Hi folks,
I am hanging around for a while with a multithreading core data app. As Apple remarks, managed object contexts are not threadsave, they suggest to create an own context for each thread. There some code snippets about that in the internet. However, they all do not match my problem.
My Thread creates a new context
the according -merge: method is as following:
The context is created properly and [ipDocument.managedObjectContext persistentStoreCoordinator] returns a value not NULL. However, when saving the document and forcing to merge the contexts by
the application crashes with
An uncaught exception was raised
This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.
As I said, this is a document based app and the matter happens short time after starting the app (with a new, empty document). What is wrong with this? Thank you in advance
I am hanging around for a while with a multithreading core data app. As Apple remarks, managed object contexts are not threadsave, they suggest to create an own context for each thread. There some code snippets about that in the internet. However, they all do not match my problem.
My Thread creates a new context
Code:
// Create context on background thread
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setUndoManager:nil];
[context setPersistentStoreCoordinator: [ipDocument.managedObjectContext persistentStoreCoordinator]];
// Register context with the notification center
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:context];
the according -merge: method is as following:
Code:
- (void)mergeChanges:(NSNotification *)notification
{
NSLog(@"merge managed contexes");
NSManagedObjectContext *mainContext = [ipDocument managedObjectContext];
// Merge changes into the main context on the main thread
[mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
}
The context is created properly and [ipDocument.managedObjectContext persistentStoreCoordinator] returns a value not NULL. However, when saving the document and forcing to merge the contexts by
Code:
NSError *error = nil;
[context save:&error];
if (error)
{
[NSApp presentError:error];
}
[context reset];
An uncaught exception was raised
This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.
As I said, this is a document based app and the matter happens short time after starting the app (with a new, empty document). What is wrong with this? Thank you in advance