核心数据:非法试图建立关系+ (空)上下文
我正在后台线程上解析数据,插入新实体并建立与其他现有实体的关系。
有时我会收到错误:“尝试在不同上下文中的对象之间建立关系”。
阅读更多相关内容后,我现在在后台线程中创建一个新的 ManagedObjectContect 并使用它来插入新实体并设置关系。我小心翼翼地只在后台线程中使用这个新的 ManagedObjectContext。当我需要找到一个现有实体来设置现有对象与这些新对象之一之间的关系时,我调用 [moc objectWithId:id] 来使用我的新 moc 获取现有对象。但是,我仍然收到错误。
沮丧的是,我开始打印东西。我注意到,当我打印每个 NSManagedObject 的 ManagedObjectContext 属性时,在设置关系之前,有时会打印出“(null)”。这似乎就是问题发生的时候。
谁能告诉我我做错了什么?为什么有时我的对象上会有(空)moc?
感谢您提供的任何帮助!
I am parsing data on a background thread, inserting new entities and setting up relationships with other existing entities.
Sometimes I get the error: 'attempting to establish relationship between objects in different contexts'.
After reading more about this I am now creating a new ManagedObjectContect in my background thread and using it to insert the new entities and setup the relationships. I am careful to only use this new ManagedObjectContext in my background thread. When I need to find an existing entity to setup a relationship between an existing object and one of these new objects, I call [moc objectWithId:id] to fetch the existing object using my new moc. However, I am still getting the error.
Frustrated, I started printing things out. I noticed that when I print out the managedObjectContext property for each NSManagedObject, just before setting up the relationship, I sometimes get '(null)' printed out. This seems to be when the problem occurs.
Can anyone tell me what I am doing wrong? Why do I sometimes have (null) mocs on my objects?
Thanks for any help you can give!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最可能的解释是您试图链接到在一个上下文中创建但尚未与另一上下文合并的对象。在合并上下文之前,在一个上下文中所做的任何更改都不会显示在另一上下文中。这就是您的错误消息告诉您的内容。
Null 对象很可能是由使用临时 objectID 引起的。在将对象保存到持久存储中之前,objectID 是不固定的。在此之前,它有一个临时 ID,该 ID 在保存时会发生变化。
The most likely explanation is that you are trying to link to objects created in one context that have not been merged with the other context. No changes made in one context will show up in another until the context have been merged. That is what your error message is telling you.
The Null object is most likely caused by using temporary objectIDs. An objectID is not fixed until the object has been saved to the persistent store. Until then it has a temporary ID which will change when it is saved.
我们需要查看您的一些代码。我的第一个想法是检查您是通过其属性还是直接访问 ManagedObjectContext ?
如果您使用 Apple 提供的模板核心数据方法,通过属性访问它将确保在创建新的托管对象时上下文始终可用(即不是 nil)。
We need to see some of your code. My first thought would be to check whether you're accessing the managedObjectContext via its property or directly?
Accessing it via property will make sure the context is always available (i.e. not nil) when creating new managed objects, provided you are using the template core data methods provided by Apple.
NSmanagementObject 有一个名为 isInserted 的方法,该方法确认对象是否已插入到 ManagedObjectContext 中,检查此值,如果没有,请使用 NSManagedObjectContext 上的 insert 方法插入它。
The NSmanagedObject has a method called isInserted, this confirms if an object has been inserted into a managedObjectContext check this value, if it is no use the insert method on NSManagedObjectContext to insert it.