Note that there are some explanatory texts on larger screens.

plurals
  1. POIllegal attempt to establish a relationship 'xyz' between objects in different contexts
    primarykey
    data
    text
    <p>I am using Apple's <code>CoreDataBooks</code> 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.</p> <p>The data I am pulling in is a <code>Book</code> entity with a to-one relationship with an <code>Owner</code> entity (called "owner"). The <code>Owner</code> entity has a to-many relationship with the <code>Book</code> (called "books").</p> <p>My data is an XML document of the form:</p> <pre><code>&lt;Owner&gt; &lt;Name&gt;alexpreynolds&lt;/Name&gt; &lt;ID&gt;123456&lt;/ID&gt; &lt;/Owner&gt; &lt;Books&gt; &lt;Book&gt;Book One&lt;/Book&gt; &lt;Book&gt;Book Two&lt;/Book&gt; ... &lt;Book&gt;Book N&lt;/Book&gt; &lt;/Books&gt; </code></pre> <p><code>Book One</code> through <code>Book N</code> are associated with one <code>Owner</code> (<code>"alexpreynolds, 123456"</code>).</p> <p>I am parsing this into an <code>Owner</code> instance and an <code>NSMutableSet</code> made up of <code>Book</code> instances.</p> <p>When I attempt to save the first time, it saves fine and the merged data shows up in the table view.</p> <p>On the second save, however, when the XML content contains a new book, it doesn't work.</p> <p>Here's what happens:</p> <p>I then attempt to load in an XML document that contains a new <code>Book</code> not already in the primary managed object context. The new <code>Book</code> is using the same <code>Owner</code> as that which is associated with the other <code>Book</code>s.</p> <p>I have routines that pick out this unique <code>Owner</code> managed object (which I already have in my primary managed object context) and the unique <code>Book</code> that is <em>not</em> found in the primary MOC.</p> <p>From this, I create a new <code>Book</code> object in the secondary MOC, and I set its "<code>owner</code>" relationship to point to the unique <code>Owner</code> I found in the primary MOC.</p> <p>When I save, I get the following error:</p> <pre><code>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'owner' between objects in different contexts (source = &lt;Book: 0x7803590&gt; (entity: Book; id: 0x7802ae0 &lt;x-coredata:/// Book/t527F06B2-3EB5-47CF-9A29-985B0D3758862&gt; ; data: { creationDate = 2009-10-12 06:01:53 -0700; name = nil; nameInitial = nil; operations = ( ); owner = nil; type = 0; }) , destination = &lt;Owner: 0x78020a0&gt; (entity: Owner; id: 0x3a56f80 &lt;x-coredata://043AF2F0-1AD0- 4078-A5E8-E9D7071D67D1/Owner/p1&gt; ; data: { books = "&lt;relationship fault: 0x7801bf0 'books'&gt;"; displayName = alexpreynolds; ownerID = 123456; }))' </code></pre> <p>How do I create a new <code>Book</code> entity in the secondary MOC, so that I can still associate it with a pre-existing <code>Owner</code> in the primary MOC?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload