Core Data Primary Key

前端 未结 7 781
無奈伤痛
無奈伤痛 2020-11-28 02:37

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

7条回答
  •  误落风尘
    2020-11-28 03:07

    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

提交回复
热议问题