Josh Kahane
Josh Kahane

Reputation: 17169

Fetching an NSManagedObject with Unique Identifier?

Do NSManagedObjects come with any kind of unique identifier?

I need to fetch a couple of objects but there is a large chance they have identical attributes, so how can I, after fetching these objects, differentiate them?

Thanks.

Upvotes: 0

Views: 765

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185841

Yes. Every NSManagedObject has an -objectId accessor which returns an NSManagedObjectID instance. These uniquely identify the object in question. You can then retrieve the object again using either of NSManagedObjectContext's methods -objectWithID: or -existingObjectWithID:error:.

Note that if the object has not yet been saved after insertion, the object ID will be a temporary ID that will change when it's saved. You can force a persistent ID to be assigned with -[NSManagedObjectContext obtainPermanentIDsForObjects:error:], although this is just as expensive as an actual save.

Upvotes: 7

Related Questions