I have an app with two managed object contexts setup like this:
When insert a new managed object to the main context, I save the main context and then the parent context like this:
[context performBlockAndWait:^{
NSError * error = nil;
if (![context save: &error]) {
NSLog(@"Core Data save error %@, %@", error, [error userInfo]);
}
}];
[parentContext performBlock:^{
NSError *error = nil;
BOOL result = [parentContext save: &error];
if ( ! result ) {
NSLog( @"Core Data save error in parent context %@, %@", error, [error userInfo] );
}
}];
My understanding is that when the manage object is first created, it has a temporary objectID
. Then the main context is saved and this object, with its temporary ID, gets to the parent context. Then the parent context is saved. When this last context is saved, the temporary objectID
in the parent context gets transformed into a permanent objectID
.
So:
[NSManagedObjectContext obtainPermanentIDsForObjects:error:]
, then background the app, reactivate it, reload, get the object using main context's objectWithID:
, and access a property, I get
"CoreData could not fulfill a fault for ...".
It is a known bug, hopefully fixed soon, but in general, obtaining a permanent ID is sufficient, provided you do so before you save the data in the first child, and you only include the inserted objects:
[moc obtainPermanentIDsForObjects:moc.insertedObjects.allObjects error:&error]
In some complex cases, it is better to get a permanent ID as soon as you create the instance, especially if you have complex relationships.
How and when are you calling obtainPermanentIDsForObjects
?
I do not follow the part about the app crashing. Maybe a better explanation would help.