Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 865
hzxu Avatar asked May 31 '12 03:05

hzxu


People also ask

Will you ever pass Managedobject from one context to another context?

You cannot pass NSManagedObjects between multiple contexts, but you can pass NSManagedObjectIDs and use them to query the appropriate context for the object represented by that ID.

What is Nsmanagedobject context?

An object space to manipulate and track changes to managed objects.

What is @NSManaged?

Behind the scenes, @NSManaged effectively means "extra code will automatically be provided when the program runs." It's a bit like functionality injection: when you say "this property is @NSManaged " then Core Data will add getters and setters to it when the app runs so that it handles things like change tracking.

What is managed object model?

A managed object model is a set of objects that together form a blueprint describing the managed objects you use in your application. A model allows Core Data to map from records in a persistent store to managed objects that you use in your application.


1 Answers

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];
like image 141
lnafziger Avatar answered Dec 04 '22 21:12

lnafziger