Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, let's look at the symptoms here. You are making changes to objects within a context, but when you restart the app, some but not all of these changes are there.</p> <p>This can only mean that these changes are not being written to the persistent store. Since you are seeing some of the changes, you are calling the <code>save:</code> method, but Core Data is not recognising all the objects as needing to be saved.</p> <p>This leads us to how you are performing the changes to said objects. Are you making the changes in a KVO-compliant manner? Core Data relies on KVO to know what changes have occurred, and therefore what changes need persisting. If you change the objects in a non-KVO fashion, they will appear to be changed when you examine them, but Core Data will not know that these changes exist, and will therefore not know that it must persist them.</p> <p>I would amend your code as follows, providing the <code>Service</code> object has the inverse relationship back to the <code>User</code> (and that relationship is called <code>user</code>, and part of a 1-M):</p> <pre><code>[servicesToAdd setValue:self.user forKey:@"user"]; NSError *error = nil; if (![[service managedObjectContext] save:&amp;error]) { NSLog(@"Saving failed"); NSLog(@"Error: %@", [error localizedDescription]); } </code></pre> <p>This relies on the fact that Core Data will take care of both ends of the relationship for you, where an inverse relationship is set up. Of course, if it's a many-to-many relationship, then the code is slightly different, but we still use the KVO-compliant methods to make the changes.</p> <pre><code>[servicesToAdd setValue:self.user forKey:@"user"]; for (Service *service in servicesToAdd) { [user addServicesObject:service]; } NSError *error = nil; if (![[service managedObjectContext] save:&amp;error]) { NSLog(@"Saving failed"); NSLog(@"Error: %@", [error localizedDescription]); } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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