How to get the identifier of an object stored in the context of an object of a managed Data Data object?

I have this code:

NSEntityDescription *userEntity = [[[engine managedObjectModel] entitiesByName] objectForKey:@"User"];
User *user = [[User alloc] initWithEntity:userEntity insertIntoManagedObjectContext:[engine managedObjectContext]];

and I want to know the identifier of the object inserted into the context of the managed object. How can i get this?

Whether this id will remain unchanged for this lifetime of the object or will it be stored in the sqlLite database below this and be what can be used for unique identification during the fetch operation (my final goal).

Any help appreciated // :)

+5
source share
3 answers

If you want to keep the object identifier forever, you need to:

  • , .
  • URI -[NSManagedObjectID URIRepresentation]. NSURL, .
  • , -[NSPersistentStoreCoordinator managedObjectIDForURIRepresentation:], NSManagedObjectID, -[NSManagedObjectContext objectWithID:] .

URI , , - , , .

, , . ObjectIDs Core Data, Data Model. Core Data , . .

, ObjectID, - . , .

+10

" "

- :

NSManagedObjectID *moID = [managedObject objectID];
+2

-, . , :

User *user = [NSEntityDescription insertEntityForName:@"User" intoManagedObjectContext:[engine managedObjectContext]];

-, , [user objectID], . "" .

( ). , , . id .

0

All Articles