Indexed Relationships in Core Data

后端 未结 3 1764
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 10:15

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

相关标签:
3条回答
  • 2020-12-24 10:18

    You could do it with another entity, like this: alt text http://gallery.me.com/davedelong/100084/Screenshot-20on-202009-07-04-20at-2010-34-56-20AM/web.jpg?ver=12467253090001

    A Document could find its actual dataItems by using something like this:

    NSSet * documentDataItems = [[document orderedDataItems] valueForKey:@"dataItem"];
    

    Likewise, a DataItem could find all its documents by doing the same:

    NSSet * dataItemDocuments = [[dataItem orderedPositions] valueForKey:@"document"];
    
    0 讨论(0)
  • 2020-12-24 10:30

    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.

    0 讨论(0)
  • 2020-12-24 10:43

    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.

    0 讨论(0)
提交回复
热议问题