这个问题看起来好像已经被问过很多次了,但是我不确定我是否已经正确汇总了答案。因此,这里去。

Apple(WWDC 2012 Session 214)将对象ID描述为上下文安全的,线程安全的。因此,我花了一些时间来转换我的代码以利用它。但是,它似乎并没有听起来那么上下文安全,因为正如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),实际上只有1个链接到持久性存储(调用为A)。因此,C是B的子代,B是A的子代。如果我从C创建一个NSManagedObject,然后调用getPermanentIDsForObjects,它实际上是永久的吗?因为.h文件注释的读取方式像是只将层次结构查找到B(支持该实体的第一个可写存储,并且在子级父级设置中,更改仅上推1级),而不是A。

提前致谢。

最佳答案

是的,如果您调用obtainPermanentIDsForObjects,那么您收到的ID应该是永久性的(除非框架中存在任何实现错误,否则在这一点上似乎不太可能实现)。

10-08 05:29