What does the `ordered` flag do in a to-many relationship?
Asked Answered
W

1

9

While looking for ways to add an ordered to-many relationship to my Core Data model, with the least possible amount of changes to the model, I noticed an option of the to-many relationship that says ordered (see screenshot below). Wow, sounds great, but what does it do?

My SQLite store is not complaining when I check or uncheck it, and my app still compiles and runs fine too. I was thinking maybe the lightweight migration takes care of the change, but from the looks of it, all my custom NSManagedObject subclasses work without the need for modification too, so what's going on?

Screenshot of the Xcode 4 Data Model Inspector

To summarize the questions:

  • Should that ordered flag change the to-many relationship's data type from NSSet to NSArray?
  • Or is it just that the order in which the set is modified, will persist on sequential reads and writes?
  • Or am I wrong with my assumptions and is it something else entirely?
  • Is there an Apple doc page where this feature is described?

Many thanks!

Wonderstricken answered 8/9, 2011 at 9:44 Comment(0)
P
11

Ordered relationships allow you to assign an arbitrary ordering to related objects. You can think of this as ordering colors from your most to least favorite, rather than sorting by date, title, etc.

Before this feature was added ordering was implemented by creating a position attribute, then manually updating the position indexes for items whenever the user reordered them. If you have a large number of items, using the built in ordering can be more expensive than implementing this manually, as described above.

Pone answered 8/9, 2011 at 21:8 Comment(5)
This kind of ordering is indeed what I'm looking for, and the position attribute would be my targeted alternative. Do you know the reason why the built-in ordering can be more expensive for a large number of items?Wonderstricken
No details were provided. However, if I had to guess, it has something to do with how the ordering is stored - such as using a separate table. I'd create a SQLite store with ordering enabled, then open the database and see how it's implemented.Pone
Thanks, for now the 'only on iOS 5' part will make the choice easy for me. I gave the answer mark to 一二三, but a +1 none the less for the performance remark. Cheers.Wonderstricken
Somehow the original answer was deleted. Maybe the user left SO. Here you go, you get a check mark!Wonderstricken
The reason automatic ordering can be more expensive is because CoreData is designed to handle any sort of data set you throw at it and thus makes no assumptions on the nature of your data. If you sort manually, you can rely on specific information you know about your data, such as which parts of it need to re-ordered and which you know to already be in the right place. Thus your manual ordering can be optimized to do the least needed amount of changes. The amount of extra coding required though may not be worth it for small data sets.Spore

© 2022 - 2024 — McMap. All rights reserved.