This may seem stupid, but I still couldn\'t figure out how to mark a attribute as a primary key in the xcdatamodel file. My persistent storage is sqlite file. Can anyone hel
Core Data makes its own primary key - you don't have to add one. You can retrieve it with
NSManagedObjectID *moID = [managedObject objectID];
Keep in mind that Core Data is an object-graph persistence framework, not a database. Things like primary keys are abstracted away because they depend on the implementation of the persistent store.
I'd rather use Time + the Class name as the unique identifier.
sometimes when hacking one needs the actual int of the primary key. here is how one can grab it:
NSManagedObjectID *yourManagedObjectID = [yourManagedObject objectID];
int yourManagedObject_PK = [[[[[yourManagedObjectID URIRepresentation] absoluteString] lastPathComponent] substringFromIndex:1] intValue];
despite CoreData being an object graph, if one looks at the CoreData generated SQLite database data, this way of grabbing the primary key of an NSManagedObject should not be a problem. ii have used CoreData and the low level sqlite3 C library together in the same code and passing primary keys from CoreData to sqlite3 for fetching records works just fine.
! if You intend to use this code in production, be aware of possible internal changes to the way the db primary key transforms into a URIRepresentation, it might brake Your code.
enjoy
Without hacking NSManagedObjectID you can perform a fast check for your attribute before manage your remote data collection.
I've made an utility method, check it here
Your options are:
-[NSManagedObject objectID]
. Note that this ID is temporary until either the object is saved for the first time or you call -[NSManagedObjectContext obtainPermanentIDsForObjects:error:]
CFUUID
family of functions to generate a UUID for each object in your -awakeFromInsert
methodThere is no good way to validate that a property is unique. The closest you'll get is to ensure it is unique at creation time, and then implement a custom setter method that stops anyone ever changing the ID.