I have a main NSManagedObjectContext
that's created in the appDelegate
.
Now, I'm using another NSManagedObjectContext
for editing/adding new objects without affecting the main NSManagedObjectContext
, until I save them.
When I save the second NSManagedObjectContext
, the changes are not reflected in the main NSManagedObjectContext
, yet if I open the .sqlite database from simulator, the changes have been saved correctly into the .sqlite database. It doesn't matter if I fetch the data again, or even if I create a third NSManagedObjectContext
, I cannot see those changes from the second NSManagedObjectContext
, despite the fact those changes do actually exist on disk at this point.
If I quit and re-open the app, all changes are there.
What can cause the main NSManagedObjectContext
to not see the new changes present in the persistent store?
Before this approach, I was using a single NSManagedObjectContext
and undoManager
, but I wanted to change it to use two different NSManagedObjectContext
s.
The second NSManagedObjectContext
save:
NSError* error = nil;
if ([managedObjectContext hasChanges]) {
NSLog(@"This new object has changes");
}
if (![managedObjectContext save:&error]) {
NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
NSLog(@" DetailedError: %@", [detailedError userInfo]);
}
}
else {
NSLog(@" %@", [error userInfo]);
}
}