Note that there are some explanatory texts on larger screens.

plurals
  1. POCoreData relationships not merging from one MOC to another
    primarykey
    data
    text
    <p>I've been banging my head against the wall with this bug. Basically relationships aren't being merged from one context to another correctly. </p> <p>Let's say I have two entity types in my model, Student and Teacher. A student can only have one teacher and a teacher can obviously have many students. I set this up correctly in my xcdatamodel class.</p> <p>Because I'm syncing these entities from the server I have 2 Managed Object Contexts - one called <code>uiContext</code> which has it its concurrency type set to <code>NSMainQueueConcurrencyType</code> and another called <code>backgroundContext</code> with its concurrency type set to <code>NSPrivateQueueConcurrencyType</code>. </p> <p>I've also set up a nested structure using <code>[self.uiContext setParentContext:self.backgroundContext];</code></p> <p>So this is what I'm doing:</p> <p>1) Fetch Teachers from the server</p> <p>2) Create managed objects for them in <code>backgroundContext</code></p> <p>3) Call <code>save</code> on <code>backgroundContext</code></p> <p>4) I listen for the <code>NSManagedObjectContextDidSaveNotification</code>, and execute the following code: </p> <blockquote> <pre><code>[self.uiContext performBlock:^(void) { [self.uiContext mergeChangesFromContextDidSaveNotification:notif]; }]; </code></pre> </blockquote> <p>So far this works just fine. My uiContext gets the Teacher managed objects and my UI updates accordingly.</p> <p>It breaks down though when I do the following:</p> <p>5) Fetch the student data from the server</p> <p>6) Create managed objects for them in the <code>backgroundContext</code> and <strong>set the relationship from student to the appropriate teacher.</strong> </p> <p>7) Call <code>save</code> on <code>backgroundContext</code></p> <p>8) Since I'm listening for the <code>NSManagedObjectContextDidSaveNotification</code> notification this save should get merged into the <code>uiContext</code></p> <p>However, here's where I start experiencing issues. Even though the student objects are merged into the <code>uiContext</code> correctly, the relationship from teacher to student is not set. The relationship from student to teacher is correctly set but not the other way around. Specifically, <code>[someTeacherObject.students count] == 0</code> but <code>someStudentObject.teacher != nil</code></p> <p>The relationships are set correctly in both the <code>backgroundContext</code> and the persistent store (verified). Just not in the <code>uiContext</code>. </p> <p>Any ideas what could be going on? Any idea how to better debug this?</p>
    singulars
    1. This table or related slice is empty.
    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