I have an app with two managed object contexts setup like this:
- Parent Context: NSPrivateQueueConcurrencyType, linked to the persistent store.
- Main Context: NSMainQueueConcurrencyType, child of Parent Context.
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:
- Does the permanent object ID ever get propagated automatically back
to the main (child) context?
- When I force to get the object permanent ID with
[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 ...".
What is wrong with this approach?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…