核心数据对象ID与永久对象ID

时间:2013-01-18 06:39:21

标签: ios objective-c core-data nsmanagedobjectcontext objectid

这个问题可能看起来像以前被问过多次,但我不确定我是否正确地汇总了答案。所以这里。

Apple(WWDC 2012 Session 214)将ObjectID描述为上下文安全的线程安全的。所以我花了一些时间来转换我的代码以利用它。但是,它看起来并不像上下文一样安全,因为正如here Core Data: Do child contexts ever get permanent objectIDs for newly inserted objects?和其他地方所讨论的那样,存在称为永久ID的东西。

关于这个永久性ID业务,我查看了NSManagedObjectContext.h:

/* Converts the object IDs of the specified objects to permanent IDs.  This implementation
will convert the object ID of each managed object in the specified array to a permanent
ID.  Any object in the target array with a permanent ID will be ignored;  additionally,
any managed object in the array not already assigned to a store will be assigned, based on
the same rules Core Data uses for assignment during a save operation (first writable store
supporting the entity, and appropriate for the instance and its related items.)  Although
the object will have a permanent ID, it will still respond positively to -isInserted until
it is saved.  If an error is encountered obtaining an identifier, the return value will be
NO.
*/

- (BOOL)obtainPermanentIDsForObjects:(NSArray *)objects error:(NSError **)error NS_AVAILABLE(10_5, 3_0);

所以我在代码中遇到了这个问题。我有一个NSManagedObjectContexts的层次结构(例如,B和C),其中只有一个实际上链接到持久存储(调用是A)。所以,C是B的子节点,B是A的子节点。如果我从C创建一个NSManagedObject,然后调用obtainPermanentIDsForObjects,它实际上是永久的吗?因为.h文件注释读起来只是将层次结构查找为B(支持实体的第一个可写商店,并且在子父设置中,更改仅向上推升1级),而不是A.

提前致谢。

1 个答案:

答案 0 :(得分:3)

是的,如果你打电话给obtainPermanentIDsForObjects,那么你收到的ID应该是永久性的(除非框架中存在任何实施错误,此时此核心似乎不太可能。)