我的应用程序使用 Core Data(在 Magical Record 的帮助下)并且使用NSOperation
.
当然,我非常小心只NSManagedObjectID
在线程/操作之间传递。
现在,为了返回操作中的相应托管对象,我使用以下方法-existingObjectWithID:error:
:
Collection *owner = (Collection *)[localContext existingObjectWithID:self.containerId error:&error];
但我得到的是 nil 并error
说这是一个错误 #13300: NSManagedObjectReferentialIntegrityError
。
以下是文档中有关此错误的说明:
NSManagedObjectReferentialIntegrityError
Error code to denote an attempt to fire a fault pointing to an object that does not exist.
The store is accessible, but the object corresponding to the fault cannot be found.
在我的情况下这是不正确的:该对象存在。实际上,如果我用 遍历该Collection
实体的所有实例NSFetchRequest
,我会在其中找到它,而它NSManagedObjectID
正是我传递给的那个-existingObjectWithID:error:
。
此外,如果我-objectWithID:
改用,我会得到一个正确的对象。
所以我缺少一些东西。以下是一些额外的观察/问题:
- “一个不存在的对象”:那句话中“存在”的含义是什么?“存在”在哪里?那时它肯定“存在”在我的核心数据存储中。
- “找不到故障对应的对象”:那句话中的“找到”是什么意思?“发现”在哪里?那时它肯定会在我的核心数据存储中“找到”。
所以也许我错过了一些关于什么的东西existingObjectWithID:error:
?文档说:
If there is a managed object with the given ID already registered in the context, that object is returned directly; otherwise the corresponding object is faulted into the context.
[...]
Unlike objectWithID:, this method never returns a fault.
这对我的问题没有帮助。我不介意让我的对象完全错误,而不是错误。事实上,当我访问对象属性时,其中的任何错误都会在下一个代码行上触发。
- 导致 的现实场景是
NSManagedObjectReferentialIntegrityError
什么?
感谢您的任何启发。