ios - How can I get the value of the primary key of an NSManagedObject? -


i found out core data sqlite database. each entity separate table. there tables not entities, including z_metadata, z_modelcache , z_primarykey. don't know do.

anyway, saw fields of "entity tables" same properties added in data model, plus few extras. example, if entity has 1 property called date. table have zdate field z_pk, z_ent, , z_opt.

so apparently, each entity has primary key stored in z_pk field! want value of primary key of specific nsmanagedobject. , want query database nsmanagedobject has specific primary key.

so basically, this:

let pk = mymanagedobject.primarykey // below should return mymanagedobject managedobjcontext.objectwithprimarykey(someentitydescription, pk) 

i searched docs nsmanagedobject , saw objectid. don't think it, or it?

core data not support doing this, because core data's api different sql. if think in terms of sql, you're going make core data more difficult needs be.

the approximate core data equivalent of code snippet (using swift 3 syntax):

let objectid = mymanagedobject.objectid  // later let mymanagedobject = context.object(with: objectid) // or... let mymanagedobject = context.existingobject(with: objectid) 

using object(with:) faster potentially less safe. using existingobject(with:) may slower safer.


Comments