views:

1065

answers:

2

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!

+1  A: 

A good solution is to keep a separate data structure in Document to map DataItems to a position in the table view. Besides allowing the same DataItem to exist in multiple positions, if you need to add a DataItem to multiple Documents this solution will also work.

Back when I was looking at different strategies of keeping Core Data objects ordered I found a blog post that explains how to do this in great detail, including sample code too.

Marc Charbonneau
+3  A: 
Dave DeLong
Picture seems to be broken.
BJ Homer
@BJ Homer - thanks; Apparently twitpic doesn't like static linking. I uploaded it to MobileMe instead.
Dave DeLong