How to create unique identifier for an entity in i

2019-06-11 20:01发布

问题:

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.