9

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

Apple (WWDC 2012 Session 214) 将 ObjectID 描述为上下文安全、线程安全的。所以我花了一些时间转换我的代码以利用它。然而,它似乎并不像听起来那样是上下文安全的,因为正如这里所讨论的 核心数据:子上下文是否会为新插入的对象获得永久的 objectID?在其他地方,存在一种叫做永久 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,然后调用 gainPermanentIDsForObjects,它实际上是永久的吗?因为 .h 文件注释读起来就像它只查找 B 的层次结构(支持实体的第一个可写存储,在子父设置中,更改仅被推高 1 级),而不是 A。

提前致谢。

4

1 回答 1

3

是的,如果您调用,obtainPermanentIDsForObjects那么您收到的 ID 应该是永久性的(除非框架中存在任何实现错误,这对于此时的核心来说似乎不太可能)。

于 2014-06-16T08:25:44.210 回答