Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexed Relationships in Core Data

I'm just starting out with using Core Data on the iPhone SDK and I'm looking into saving an ordered list, something like an array. However, relationships in Core Data are expressed as Sets when retrieved. This makes it difficult to save the order in which the objects are positioned.

A good example would be data items in table view when re-ordering of items are allowed. An easy solution would be to include an index property on the managed object.

Consider the following hierarchy:

Document <-Many-to-many-> DataItem

Different Document instances could link to the same DataItem, and each Document might reference one or more DataItem(s). Hence, having an index property in DataItem would lead to less reusability of that instance, i.e. you can only save the index for one instance of Document.

Any ideas of how I can present the hierarchy neatly ordered in a table view but still keep each DataItem instance reusable? Thanks!

like image 923
Matthew Avatar asked Jul 04 '09 12:07

Matthew


1 Answers

As of OS X Lion (10.7) this is now much more straight forward. Cocoa now has support for a new NSOrderedSet class which is compatible with Core Data. This functionality is also available in iOS though requires iOS 5.0 or later. This means that this can't be used if you want your app to be backwards compatible with earlier versions of iOS or OS X.

All you need to do to gain ordering is open the Core Data model editor, select a to-many relationship and check the "ordered" check-box.

like image 195
Benjamin Dobell Avatar answered Oct 26 '22 07:10

Benjamin Dobell