Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am not exactly clear on whether you need to save an entity name or a particular managed object but in both cases the solution is the same: you need to store a reference in the NSUserDefaults. </p> <p>To store an entity in the user defaults you would just save the entity name as a string. So something like:</p> <pre><code> [[NSUserDefaults standardUserDefaults] setValue:entityNameString forKey:@"currentlySelectedEntity"]; </code></pre> <p>... and to retrieve it:</p> <pre><code> NSString *currentEntity=[[[NSUserDefaults standardUserDefaults] valueForKey:@"currentlySelectedEntity"]; </code></pre> <p>If you need to save a reference to a particular managed object, you need to save the managed object ID of the <strong><em>saved</em></strong> object. Saving is very important. Prior to saving the context in which the object was created, the object ID has only a temporary value which will change upon saving. Any reference to object will be lost if you use the temporary value. </p> <p>To save an object ID:</p> <pre><code> NSManagedObject *mo=[NSEntityDescription insertNewObjectForEntityForName:@"TestEntity" inManagedObjectContext:self.managedObjectContext]; //... save the context NSManagedObjectID *moID=[mo objectID]; NSURL *moIDURI=[moID URIRepresentation]; [[NSUserDefaults standardUserDefaults] setValue:moIDURI forKey:@"currentlySelectedTestObject"]; </code></pre> <p>... to retrive it:</p> <pre><code> NSURL *uri=[[NSUserDefaults standardUserDefaults] valueForKey:@"currentlySelectedTestObject"]; NSManagedObjectID *moID=[self.persistentStoreCoordinator managedObjectIDForURIRepresentation:uri]; NSManagedObject *mo=[self.managedObjectContext objectRegisteredForID:moID]; </code></pre> <p>(The above code is uncompiled and may contain typos so don't just copy and paste it.)</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.
    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