kylef-archive / KFData

Core Data done right for iOS 5+/OS X 10.7+
http://cocoadocs.org/docsets/KFData/
BSD 2-Clause "Simplified" License
53 stars 7 forks source link

Generate unique ObjectID's based on a web services unique ID #26

Closed kylef closed 10 years ago

kylef commented 11 years ago
x-coredata://96EBBFDE-9EED-4AE1-BEA3-B7CA019E1402/PutIOFile/p2

x-coredata://<persistent store identifier>/<entity name>/p<key>

So all we would need to do is grab the persistent store identifier for the store we want to store the entity in. Then insert the entity name and unique ID from an API. I'm guessing the P is for primary key.

This would ensure we always have a unique entity for any upstream ID.

(lldb) po [[[[managedObjectContext persistentStoreCoordinator] persistentStores] objectAtIndex:0] identifier]
$12 = 0x09510ce0 96EBBFDE-9EED-4AE1-BEA3-B7CA019E1402

$26 = 0x087556f0 <PutIOFile: 0x87556f0> (entity: PutIOFile; id: 0x930ef20 <x-coredata://96EBBFDE-9EED-4AE1-BEA3-B7CA019E1402/PutIOFile/p2> ; data: <fault>)

25

calvincestari commented 11 years ago

What is the justification behind coming up with our own ID generation though. It's not a requirement that an API index it's objects nor can we rely on it.

kylef commented 11 years ago

I'm not quite sure if I understand this question correctly, does the attached commit answer your question?

This means that any API client should use the objectWithPrimaryKey:inManagedObjectContext: method instead of having its own "unique id" parameter on an entity. — Sent from iPhone

On Mon, Apr 15, 2013 at 2:37 AM, Calvin Cestari notifications@github.com wrote:

What is the justification behind coming up with our own ID generation though. It's not a requirement that an API index it's objects nor can we rely on it.

Reply to this email directly or view it on GitHub: https://github.com/kylef/KFData/issues/26#issuecomment-16363786

calvincestari commented 11 years ago

My question was 'what is being passed in from the client as (NSUInteger)primaryKey'? Is this the position within an array? If so, what about a parsed top-level NSDictionary object that has no position?

kylef commented 11 years ago

Primary key is the primary key an API would use. Example code from an API:

NSDictionary *JSONDictionary;

NSNumber *objectID = [JSONDictionary valueForKey:@"id"];  // = @4;
Person *person = [Person objectWithPrimaryKey:objectID inManagedObjectContext:managedObjectContext];
calvincestari commented 11 years ago

Yeah, I get what you were meaning now.