views:

60

answers:

2

Example: I read data from an XML file. This data has unique id elements. I want to store those objects with their original unique id. How would I do that?

I figured out I could ask the managed object for it's ID, like this:

NSManagedObjectID *moID = [managedObject objectID];

but here the problem is: The XML tells me with the id element which object this is, and I need to look up in the database of core data if this object already exists in there, or not. So is it the only option to make an id attribute in my managed object model for that entity and then query for that? Then I will have two id systems right?

+1  A: 

In the entity associated to this kind of objects, simply add another attribute of type string, call it objectID or similar and declare it to be mandatory.

unforgiven
I don't get it. Can you give an example?
dontWatchMyProfile
Open your Core Data model, select your entity and add a new attribute as I told you. Note that the ObjectID as explained by Diederick has nothing to do with what you want to achieve: assigning your own personal ID to your objects. When you insert a new object, then you set the ID attribute using your own string that uniquely identifies the object for your purposes.
unforgiven
Thank you. Just to get that right: I would simply create an attribute of type string (just like for the firstName, for example) and mark it as "indexed" so that it can be looked up quickly. And I won't care about that "random" ID which core data generates. Right?
dontWatchMyProfile
Right. Core data will use its own ID to identify the object, but you will rely on your own.
unforgiven
+2  A: 

Don't worry about the ObjectID of Core Data. This is an internal unique ID which is not guarantied to be constant during the object's life cycle (e.g. it will change when you save the object to sql store). Just create a new mandatory attribute in your model and flag it as indexed so retrieval will be fast.

Diederik Hoogenboom