How to create unique identifier for an entity in iphone core data

99封情书 提交于 2020-01-24 00:45:10

问题


I am new to core data and iphone programming so dont mind my silly qn. I couldn't figure out how to mark a attribute as a primary key in the xcdatamodel file or may be outside. I looked around and found some people saying that it is not possible.

I am getting identifier for every object that i am getting, i would need to store that object through core data and i might need to use that identifier in my web service to retrieve additional data. How do i keep the primary key of an entity equal to that identifier, any help is greatly appreciated.

Thanks in advance,

Yogesh


回答1:


In short you cannot mark an attribute as primary key automatically. You have maintain one by your own.

You can do anyone of the below:

Use [NSManagedObject objectID].

Your own primary key-like system that stores an integer in your model and increments it with the creation of each object




回答2:


You shouldn't be thinking about Core Data as a database structure with tables and keys. That's just an implementation detail.

To get an entity with a partucular you create a fetch request into your data store for that identifier and it will retrieve that object into a managed object context.

If you are worried about performance, you can make the attribute that stored this identifiers as indexed in the data model.



来源:https://stackoverflow.com/questions/10189696/how-to-create-unique-identifier-for-an-entity-in-iphone-core-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!