iPhone Core Data Fetch Primary Key

给你一囗甜甜゛ 提交于 2019-12-01 23:02:10

CoreData manages persistent object graphs. It is not a RDBM. You have no control over the unique primary key, so you have no control over its value. It is merely an implementation detail, and should not be referenced for any reason.

Thus, why do you want to sort based on it? I can think of several reasons...

  1. You want to access the objects in the order in which they were added to the database. However, you can easily do this in several ways.

1a. You can add a field to each Entity. You can set it to be a UUID, or in increasing integer.

1b. You could have an entity, something like "EntityList" which has an ordered one-to-many relationship to "Entity."

1c. You could add a one-to-one relationship from Entity to itself, then just manage it like a linked list. Every time you add an Entity, put it at the end of the list.

Either of this solutions give you the ability to access the Entity objects in order they were added.

  1. Or, you really don't care the order you see them, but you want a consistent ordering.

2a. Add a primaryKey attribute, and assign it a UUID.

2b. Use the ordered to-many relationship.

Finally, if you really want primary-key-based access, I would suggest having a separate sqlite database that you use for special keyed access. You can then associate a key to the objectID of an object in the CoreData store.

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