hzxu
hzxu

Reputation: 5823

objectID of NSManagedObject: is it consistent?

I need to have some unique&consistent ID for indexing data, I tried to use objectID of NSManagedObject, but looks like for the same entity, its objectID keeps changing, does anyone know if this is not consistent?

Upvotes: 12

Views: 8069

Answers (1)

lnafziger
lnafziger

Reputation: 25740

Unless you haven't saved a new object, the objectID is unique and consistent.

To quote the Core Data Programming Guide:

Managed Object IDs and URIs

An NSManagedObjectID object is a universal identifier for a managed object, and provides basis for uniquing in the Core Data Framework. A managed object ID uniquely identifies the same managed object both between managed object contexts in a single application, and in multiple applications (as in distributed systems). Like the primary key in the database, an identifier contains the information needed to exactly describe an object in a persistent store, although the detailed information is not exposed. The framework completely encapsulates the “external” information and presents a clean object oriented interface.

NSManagedObjectID *moID = [managedObject objectID]; 

There are two forms of an object ID. When a managed object is first created, Core Data assigns it a temporary ID; only if it is saved to a persistent store does Core Data assign a managed object a permanent ID. You can readily discover whether an ID is temporary:

BOOL isTemporary = [[managedObject objectID] isTemporaryID];

Upvotes: 22

Related Questions