circuitlego
circuitlego

Reputation: 3479

Core Data Reverse Ordered Relationship Fetch

I would like to be able to fetch all entities of a to-many relationship that is ordered. However, I need them to be in the reverse order. I'm using a fetchedResultsController and don't know how to accomplish this. The ordered relationship works, but I want it descending, rather than ascending.

Thanks in advance.

Upvotes: 0

Views: 665

Answers (1)

Puneet Sharma
Puneet Sharma

Reputation: 9484

You must be using a Sort Desrciptor to fetch the values in ascending order.

+ (id)sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending

If you pass YES as a BOOL to ascending , then it returns a sortDesriptor that sorts ascendingly.
If you pass NO as a BOOL to ascending , then it returns a sortDesriptor that sorts descendingly.

EDIT: If I am not wrong then Core Data returns NSOrderedSet for ordered to-many relationships. You can use this method on returned NSOrederedSet object to get a reverse NSOrderedSet

NSOrderedSet *reversedResutsSet = [yourOrderedSet reversedOrderedSet];

Hope this helps.

Upvotes: 3

Related Questions