核心数据:在这种情况下,NSManagedObject永远不会获得永久ID

时间:2013-07-30 18:26:37

标签: ios core-data nsmanagedobject

使用核心数据,我在各种方法之间传递objectID以执行各种操作。 我似乎有一个错误,因为对象ID是临时的,即使强迫它成为永久对象。

NSManagedObjectContext *context=[(id)[[UIApplication sharedApplication] delegate] theContext];
__block MyObject *myObject;
[context performBlockAndWait:^(void){
    myObject= [NSEntityDescription insertNewObjectForEntityForName:@"MyObject" inManagedObjectContext:context];
    myObject.name=@"thename";
    [(id)[[UIApplication sharedApplication] delegate] saveTheContext];
}];
NSLog(@"is temporary before calling obtainpermanentid=%d",[myObject.objectID isTemporaryID]);
BOOL b=[context obtainPermanentIDsForObjects:[NSArray arrayWithObject:myObject] error:nil];
NSLog(@"is temporary after calling obtainpermanentid=%d",[myObject.objectID isTemporaryID]);

在这种情况下,我希望在保存后获得永久的objectID。 它没有发生 我试图通过调用obtainPermanentIDsForObjects(返回true)强制执行此操作,但此后对象仍然有一个临时ID。

关于什么条件可能导致对象ID保持临时性的任何想法?

1 个答案:

答案 0 :(得分:0)

我的不好,这段代码是在使用NSIncrementalStore的现有实现(通过AFIncrementalStore)之上添加的。

作为子类化NSIncrementalStore的一部分,方法obtainPermanentIDsForObjects被覆盖,在这种情况下,该方法在特定条件下返回临时对象ID。