核心数据-existingObjectWithID:错误:导致错误133000 [英] Core Data -existingObjectWithID:error: causes error 133000

查看:218
本文介绍了核心数据-existingObjectWithID:错误:导致错误133000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用Core Data(使用Magical Record的一些帮助),并且使用 NSOperation 进行相当严重的多线程。



当然,我非常小心,只在线程/操作之间传递 NSManagedObjectID



回到操作中的相应托管对象,我使用 -existingObjectWithID:error:

  Collection * owner =(Collection *)[localContext existingObjectWithID:self.containerId error:& error]; 

但是我得到的是nil和错误说这是一个错误#13300: NSManagedObjectReferentialIntegrityError



这里是文档说的这个错误: / p>

<$ c $ p> NSManagedObjectReferentialIntegrityError
表示尝试触发指向不存在的对象的故障的错误代码。
可访问存储,但找不到与故障相对应的对象。

在我的情况下不是真的:那个对象存在。事实上,如果我用 NSFetchRequest 遍历 Collection 实体的所有实例,我发现它在其中, NSManagedObjectID 正是我传递给 -existingObjectWithID:error:



此外,如果我使用 -objectWithID:,我得到一个正确的对象,只是罚款。



所以有一些我错过了。以下是一些其他的意见/问题:




  • 不存在的对象:句子? 存在在哪里?

  • 无法找到对应于故障的对象:该句子中found的含义是什么? 发现在哪里?



所以也许我缺少一些关于 existingObjectWithID:error:是吗?文档说:

 如果有上下文中已经注册了给定ID的托管对象,否则相应的对象被故障到上下文中。 
[...]
与objectWithID:不同,此方法不会返回错误。

这对我的问题没有帮助。我不介意让我的对象完全故障,而不是故障。事实上,当我访问对象属性时,其中的任何错误都会在下一个代码行上触发。




  • NSManagedObjectReferentialIntegrityError



感谢任何启发。

$问题是,你传递的 NSManagedObjectID 是临时的。您可以通过调用 NSManagedObjectID isTemporaryID 方法来检查它。来自docs:


返回一个布尔值,表示
接收方是否是临时的。



大多数对象ID返回NO。新对象插入
被管对象上下文中被分配一个临时ID,一旦对象被保存到持久化存储中,它被替换为永久的一个临时ID。


您应该先将更改保存到持久存储,然后再获取永久ID以传递给其他上下文。


My app uses Core Data (with some help of Magical Record) and is rather heavily multithreaded using NSOperation.

Of course I am very careful to only pass around NSManagedObjectID between threads/operations.

Now, to get back to the corresponding managed object in an operation, I use -existingObjectWithID:error: thus:

Collection *owner = (Collection *)[localContext existingObjectWithID:self.containerId error:&error];

But what I get back is nil and error says this is an error #13300: NSManagedObjectReferentialIntegrityError.

Here is what the documentation says about this error:

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.

Which is not true in my case: that object exists. Indeed, If I iterate through all instances of that Collection entity with an NSFetchRequest, I find it among them, and its NSManagedObjectID is exactly the one I passed to -existingObjectWithID:error:.

Moreover, if I use -objectWithID: instead, I get a correct object back just fine.

So there is something I'm missing. Here are a few additional observations/questions:

  • "an object that does not exist": what it the meaning of "exist" in that sentence? "exist" where? It definitely "exists" in my Core Data store at that point.
  • "the object corresponding to the fault cannot be found": what it the meaning of "found" in that sentence? "found" where? It definitely "be found" in my Core Data store at that point.

So maybe I am missing something regarding what existingObjectWithID:error: does? The documentation says:

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.

This doesn't help my issue. I don't mind getting my object fully faulted, and not a fault. In fact, any fault within it will fire on the next code line when I access the object properties.

  • What would be a realistic scenario leading to an NSManagedObjectReferentialIntegrityError?

Thanks for any enlightenment.

解决方案

The problem is that NSManagedObjectID you pass is temporary. You can check it by calling NSManagedObjectID's isTemporaryID method. From docs:

Returns a Boolean value that indicates whether the receiver is temporary.

Most object IDs return NO. New objects inserted into a managed object context are assigned a temporary ID which is replaced with a permanent one once the object gets saved to a persistent store.

You should first save your changes to persistent store, only then get a permanent ID to pass to other context.

这篇关于核心数据-existingObjectWithID:错误:导致错误133000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆