开发者

Can I tell Core Data to use a specific unique ID for an y object when saving it?

开发者 https://www.devze.com 2022-12-20 03:29 出处:网络
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?

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 e开发者_开发问答ntity and then query for that? Then I will have two id systems right?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号