0

I have an entity call Company, it has 2 property companyName and companyID.

I want to set ID for my object by finding the highest number ID then adding 1 to it.

But the problem is that i dont know how to set ID for the first object.

Anyone know how to solve that or have a better way to manage data by ID or something else in coredata, please let me know.

  • 1
    Keep a constant in `NSUserDefauts`, this constant knows your last assigned value. Assign this value and increment it and save it back. – Sachin Vas Oct 13 '16 at 14:29
  • 1
    You can ask your DataBase what's the highest companyID using `@max` in a fetch on start, and keep increasing its value when needed. – Larme Oct 13 '16 at 14:33
  • Here is some lecture to get the highest value at launch http://stackoverflow.com/questions/10398019/core-data-how-to-fetch-an-entity-with-max-value-property Then keep it somewhere (static int, var into a singleton, etc.) – Larme Oct 13 '16 at 18:13

0 Answers0