user1007895
user1007895

Reputation: 3965

Unique Identifier for iOS core data object?

I have an iPhone app using Core Data, but I can't seem to figure out a solid unique identifier to a particular entity. Here is the situation:

I have a Profile entity, with relationships to other entities. At the start of the app, the user with select a Profile to "sign in" with. I need to keep track of that particular profile throughout the use of the app so I can fetch the right related objects.

If I were using a normal sql database, I would just store the profileID (primary key) as a property in the App Delegate. But, in Core Data, there is no primary key.

I know there is a managed object objectID, but from what I read, it can change (A user can change their profile info while using the app, meaning it has to be fetched then re-saved). Is there a best practice to handle situations like this that I am unaware of?

Upvotes: 3

Views: 5367

Answers (2)

adonoho
adonoho

Reputation: 4339

user1007895,

There is a mechanism to persist a permanent objectID. -[NSManagedObjectID URIRepresentation] and -[NSPersistentStoreCoordinator managedObjectIDForURIRepresentation:]. These are guaranteed to never change.

Andrew

Upvotes: 7

TheEye
TheEye

Reputation: 9346

Re-saving won't change the unique objectID, except when you re-create (= copy) the object and save it - the unique ID would be pretty senseless otherwise.

If that would be the case, you can always have your own id field, which you could fill with the objectID the first time a profile is created; that field would never ever change if you don't change it yourself :-).

Upvotes: 1

Related Questions