Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS >> CoreData >> Should I Create an ID Attribute or Can I Use the SQLite Object ID?

When working with CoreData, do I need to create an ID attribute to have some sort of unique key for each record or can I access the data base ID field per each line in the table?

If there's a way to use the DB ID field, I would appreciate some direction about how to get to it when saving the record via the managedObject instance...

like image 524
Ohad Regev Avatar asked Oct 08 '13 10:10

Ohad Regev


Video Answer


1 Answers

Usually there is no need to create a id attribute because every object has an object ID (NSManagedObjectID). If you have a managed object you get it's id by sending -objectID to it. There is a catch though:

There is a catch: An NSManagedObjectID can be either temporary or permanent. When an object is created it has a temporary ID until you obtain a permanent ID for the object. When you persist an object Core Data automatically obtains a permanent ID for each object which still have temporary IDs.

If you need a custom ID attribute you should create it for each entity individually. Do not create an abstract entity with an ID attribute. If you do that Core Data will create a huge (monster) table for all entities that inherit from the abstract entity. This is bad.

like image 59
Christian Kienle Avatar answered Nov 15 '22 12:11

Christian Kienle