26
votes

When adding a relationship within core data, there is an option there to make the relationship "Ordered" (the checkbox). How does core data manage the ordering of relationship or what is the default ordering (if there is any)?

Say, I got an Header and Detail entities. I specified a "To-Many" relationship (Header can have many Detail) and checked the "Ordered" check box. When I do a fetch on the "Header", I can specify which attribute it will be ordered -- I'm ok with this and my question is not related to this. I put the result on a tableview and when user tapped detail disclosure, I will display the Detail in another tableview. I don't use a FetchResultController to display details, I just access the details via relationship. This means I don't specify any ordering for the details. My question is related to this, how does core data manage the ordering of the Details? Since I specified the relationship as "Ordered", does it really order the details by default? If yes, what is the default order behavior?

1

1 Answers

28
votes

The ordered setting just creates the property as an NSOrderedSet within that entity. So essentially, they are ordered based on the order in which they are added.

You can sort the set yourself using sortedArrayUsingComparator:

More info: https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSOrderedSet_Class/Reference/Reference.html