I\'m working on a Cocoa-Touch app, it uses CoreData and has some NSPersistentObject subclasses generated by the XCode model editor.
I\'ve noticed that recently, when
I have another example of how to cause this problem: I have a MOC with a concurrency type of NSMainQueueConcurrencyType
. Somewhere in the code I do this:
__block MyObjectType1 *obj1;
[managedObjectContext performBlockAndWait:^{
obj1 = [NSEntityDescription insertNewObjectForEntityForName:@"Thing" inManagedObjectContext:managedObjectContext];
}];
// some other stuff
[self saveContext];
__block NSManagedObjectID *object1ID;
[managedObjectContext performBlockAndWait:^{
object1ID = [obj1 objectID];
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// do some slow-ish stuff
[managedObjectContext performBlockAndWait:^{
// create new object that has a relationship
NSManagedObject *obj1_copy = [managedObjectContext objectWithID:object1ID];
MyObjectType2 *obj2 = [NSEntityDescription insertNewObjectForEntityForName:@"OtherThing" inManagedObjectContext:managedObjectContext];
obj2.relatedThing = obj1_copy;
}];
[self saveContext];
});
It turns out that sometimes, this fails. I still don't understand why, but forcing to get a non-temporary objectID
seems to do the trick:
[context performBlockAndWait:^{
NSError *error;
[managedObjectContext obtainPermanentIDsForObjects:@[obj1] error:&error];
object1ID = obj1.objectID;
}];