Core Data primary key ID for a row in the database

42,037

Solution 1

-[NSManagedObject objectID] is the unique ID for an object instance in Core Data. It can be serialized via -[NSManagedObjectID URIRepresentation]. You can retrieve the objectID from a persistent store coordinator with -[NSPersistentStoreCoordinator managedObjectIDForURIRepresentation:] and then get the object from a managed object context with -[NSManagedObjectContext objectWithID:].

BUT

You should keep in mind that Core Data is not an ORM. It is an object graph management framework. That is uses SQLite (and unique row IDs) as a backend is purely an implementation detail. The sooner you can get yourself out of the SQL/RDBMS mindset, the faster you will be happy with Core Data. Instead of trying to find an object from a stored ID, consider why you need that object and what object needs it. If an instance of class Foo needs to be able to get to an instance of class Bar, why not just create an association from the Foo to the Bar and set the appropriate Bar instance as the target of the association on the appropriate Foo instance. Let Core Data keep track of object IDs.

Solution 2

As Barry Wark said, remember always that Core Data is not an orm. Pure SQL details are not exposed to the user and every row is just an object. By the way, sometime you should need to access the "primary key", for example when you need to sync the coredata db with external sql databases (in my case I needed it in a callback function to change the state of an object after INSERT it with success in the remote db). In this case, you can use:

objectId = [[[myCoredataObject objectID] URIRepresentation] absoluteString]

that will return a string like: x-coredata://76BA122F-0BF5-4D9D-AE3F-BD321271B004/Object/p521 that is the unique id used by Core Data to identify that object.

If you want to get back an object with that unique id:

NSManagedObject *managedObject = [managedObjectContext objectWithID:[persistentStoreCoordinator managedObjectIDForURIRepresentation:[NSURL URLWithString:objectId]]];

NB: Remember that if the receiver has not yet been saved in the CoreData Context, the object ID is a temporary value that will change when the object is saved.

Share:
42,037
WoodenKitty
Author by

WoodenKitty

Meow

Updated on November 20, 2021

Comments

  • WoodenKitty
    WoodenKitty over 2 years

    Suppose I have a list of books stored in Core Data. I want to search for a book by it's primary key ID.

    I know the sqlite file created by Core Data has an ID column in each table, but this doesn't seem to be exposed to me in anyway.

    Does anyone have any recommendations?

  • Abizern
    Abizern almost 13 years
    I wish I could give you another plus for 'Core Data is not a database'.
  • Amogh Talpallikar
    Amogh Talpallikar over 11 years
    exactly! the same problem. needed to send the server this id with entity details to make an entry into remote mySQL and then in response, get the server generated unique id with the same objectID. get the object form objectId and store the server id in it.
  • memmons
    memmons over 11 years
    There are common use cases where you need to grab an object by its id -- passing data from a table view cell selection to a detail view controller comes to mind. You could just pass the object itself, but often you don't want to do that if you are using a different context in the detail VC.
  • Noah
    Noah over 10 years
    @Michael: Exactly. What is the best practice for doing this?
  • Ahmed Z.
    Ahmed Z. over 10 years
    Thank you for the nyc explanation and code… solved my problem.. :)
  • Pavan
    Pavan over 10 years
    @donnit, amogh So what do I do in a situation where I am saving content offsite, and then when I need to populate the server end to then be able to match Id's and update values? and also be able to add a new object if the id doesn't exist in the database. How would I accomplish that then?
  • Shai
    Shai over 10 years
    @Noah passing objectIDs is one of the 2 best practices according to Apple.
  • Jason Foglia
    Jason Foglia over 7 years
    How does one manage objectIDs in a server->client situation where syncing is necessary?
  • user965972
    user965972 over 2 years
    You don’t. Treat managedObjectID’s as local to the store and pass the data between client and server. Add your own ID field as necessary. CoreData uses SQLite which is an embedded database, not a distributed one.