Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordered Core Data Relationship - How it is managed or what is the default ordering?

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?

like image 488
apisip Avatar asked Oct 06 '13 11:10

apisip


People also ask

What are relationships in Core Data?

Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.

What is a Core Data fetched property?

Fetched Properties in Core Data are properties that return an array value from a predicate. A fetched property predicate is a Core Data query that evaluates to an array of results.

How do I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

What is Core Data in swift iOS?

Core Data is a graphical and persistence framework, which is used in Apple devices with operating systems macOS and iOS. Core Data was first introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0.


1 Answers

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

like image 184
iwasrobbed Avatar answered Oct 02 '22 17:10

iwasrobbed