非法尝试在不同上下文中的对象之间建立关系“xyz” [英] Illegal attempt to establish a relationship 'xyz' between objects in different contexts

查看:145
本文介绍了非法尝试在不同上下文中的对象之间建立关系“xyz”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Apple的 CoreDataBooks 示例应用程序作为在后台将数据拉入辅助托管对象上下文的基础,然后将该数据合并到主要托管对象上下文。



我收到的数据是与<$ c有一对一关系的 Book $ c> Owner 实体(称为owner)。 Owner 实体与 Book (称为books)有一对多的关系。



我的数据是以下形式的XML文档:

 < Owner& 
< Name> alexpreynolds< / Name>
< ID> 123456< / ID>
< / Owner>
< Books>
< Book> Book One< / Book>
< Book> Book Two< / Book>
...
< Book> Book N< / Book>
< / Books>

Book One Book N 与一个 Owner alexpreynolds,123456)相关联。 / p>

我将此解析为 Owner 实例和 NSMutableSet Book 实例。



当我第一次尝试保存时,它保存得很好,



然而,在第二次保存时,当XML内容包含新书时,它不起作用。



这是发生了什么:



然后我尝试加载一个XML文档,其中包含一个新的 / code>尚未在主管理对象上下文中。新的 Book 正在使用与其他 Book 相关联的 Owner / code> s。



我有例程选择了这个独特的 Owner 托管对象已经在我的主要管理对象上下文中)和在主要MOC中找到的唯一 Book



从这里,我在辅助MOC中创建一个新的 Book 对象,并且设置它的所有者关系指向在主要MOC中找到的唯一所有者



我收到以下错误:

  ***由于未捕获而终止应用程序
exception'NSInvalidArgumentException',
原因:非法尝试在
中的对象之间建立
关系所有者不同的上下文

(源=< Book:0x7803590>
Book; id:0x7802ae0< x-coredata:///
Book / t527F06B2-3EB5-47CF-9A29-985B0D3758862>
; data:{
creationDate = 2009-10-12 06:01:53 -0700;
name = nil;
nameInitial = nil;
operations =(
);
owner = nil;
type = 0;
}),

destination =< Owner:0x78020a0> (entity:
Owner; id:0x3a56f80< x-coredata:// 043AF2F0-1AD0-
4078-A5E8-E9D7071D67D1 / Owner / p1>; data:{
books =< ; relationship fault:0x7801bf0'books'>;
displayName = alexpreynolds;
ownerID = 123456;
}))'

如何在辅助MOC中创建新的 Book 实体,以便我仍然可以将其与

解决方案

您可以在主要MOC中预先存在 t在不同的受管理对象上下文中的对象之间具有关系。因此,一种方法是将对象置于受管对象上下文中。



例如:

  NSManagedObject * book = //在一个MOC中获取一本书
NSManagedObject * owner = //获取不同MOC中的所有者
[[owner mutableSetValueForKey:@ books] addObject:[owner.managedObjectContext objectWithID:[book objectID]]];

因此,你所做的是获取 Book 进入与 owner 相同的受管对象上下文。但请记住,这只有在 book 已保存时才可能。管理对象上下文将寻找持久存储中的对象,因此必须首先保存。


I am using Apple's CoreDataBooks sample application as a basis for pulling data into a secondary managed object context in the background, and then merging that data into the primary managed object context.

The data I am pulling in is a Book entity with a to-one relationship with an Owner entity (called "owner"). The Owner entity has a to-many relationship with the Book (called "books").

My data is an XML document of the form:

<Owner>
  <Name>alexpreynolds</Name>
  <ID>123456</ID>
</Owner>
<Books>
  <Book>Book One</Book>
  <Book>Book Two</Book>
  ...   
  <Book>Book N</Book>
</Books>

Book One through Book N are associated with one Owner ("alexpreynolds, 123456").

I am parsing this into an Owner instance and an NSMutableSet made up of Book instances.

When I attempt to save the first time, it saves fine and the merged data shows up in the table view.

On the second save, however, when the XML content contains a new book, it doesn't work.

Here's what happens:

I then attempt to load in an XML document that contains a new Book not already in the primary managed object context. The new Book is using the same Owner as that which is associated with the other Books.

I have routines that pick out this unique Owner managed object (which I already have in my primary managed object context) and the unique Book that is not found in the primary MOC.

From this, I create a new Book object in the secondary MOC, and I set its "owner" relationship to point to the unique Owner I found in the primary MOC.

When I save, I get the following error:

*** Terminating app due to uncaught 
exception 'NSInvalidArgumentException', 
reason: 'Illegal attempt to establish a 
relationship 'owner' between objects in 
different contexts 

(source = <Book: 0x7803590> 
(entity: Book; id: 0x7802ae0 <x-coredata:///
Book/t527F06B2-3EB5-47CF-9A29-985B0D3758862>
; data: {
creationDate = 2009-10-12 06:01:53 -0700;
name = nil;
nameInitial = nil;
operations = (
);
owner = nil;
type = 0;
}) , 

destination = <Owner: 0x78020a0> (entity: 
Owner; id: 0x3a56f80 <x-coredata://043AF2F0-1AD0-
4078-A5E8-E9D7071D67D1/Owner/p1> ; data: {
books = "<relationship fault: 0x7801bf0 'books'>";
displayName = alexpreynolds;
ownerID = 123456;
}))'

How do I create a new Book entity in the secondary MOC, so that I can still associate it with a pre-existing Owner in the primary MOC?

解决方案

You can't have relationships between objects in different managed object contexts. So one way of getting around that is to bring the object into the managed object context.

For example:

NSManagedObject *book = // get a book in one MOC
NSManagedObject *owner = // get an owner in a different MOC
[[owner mutableSetValueForKey:@"books"] addObject:[owner.managedObjectContext objectWithID:[book objectID]]];

So what you're doing is actually fetching the Book into the same managed object context with owner. Keep in mind, though, that this is only possible if book has already been saved. The managed object context is going to look for the object in the persistent store, so it has to be saved first.

这篇关于非法尝试在不同上下文中的对象之间建立关系“xyz”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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