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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With