Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordered CoreData records

As I know CoreData does not preserve order of records as they were added. So if we want to store array of objects in CoreData we should add field like orderId to object and fill it manually. But it looks that underlaying sqlite base stores index for records, so may be there is a way to store ordered objects without adding extra field?

like image 829
Vladimir Avatar asked Jan 18 '23 12:01

Vladimir


1 Answers

Short answer: NO. Long answer:

  • in order to preserver order, order information must be stored. It can be implicit, using a sort descriptor on an existing property as suggested by ShiShi. Or you can add a new property for the explicit purpose of maintaining a sort order on the instances.

  • SQLite does maintain an implicit primary key, rowId, if one is not defined by the client code. But if the client code defines an integer primary key, SQLite will use that as its rowId. In other words, there is no advantage in space or time to try to be clever and use SQLite's rowId. Note that you can tell Core Data that your property should be indexed. Core Data may or may not be defining another primary key.

  • Trying to depend on how Core Data implements its persistent store is a **BAAAAD** idea: your code will break compatibility with other stores, past present or future, and will likely break with a future update.

  • You may have noticed that Core Data now supports ordered relationships with Mac OS X Lion or iOS 5. The underlying implementation most probably uses a specific additional column to store ordering information.

like image 87
Jean-Denis Muys Avatar answered Jan 25 '23 09:01

Jean-Denis Muys