NSSortDescriptor and to-many relationships

cocoa-touch, core-data, iphone

Solution

AFAIK, you must only use collection operators, described in the Key-Value Coding Programming Guide, for the key paths of your sort descriptors with collections (`NSArray`, `NSSet`, `NSDictionary`).

You must not use collection operators for the key paths of your sort descriptors with fetch requests (`NSFetchRequest`).

@kdbdallas correctly uses a collection operator to sort an `NSArray`. He is not using it to specify the sort descriptor of a fetch request.

It'd be sweet though if collection operators worked for specifying the key paths by which to sort fetch requests. Please submit this feature request at http://bugreport.apple.com/. The more it's reported, the more likely they'll support it.

Problem

I have two types of objections: locations and history items. I'm trying to fetch locations which are attached to any history item, so my fetch predicate for the location is "history.@count > 0", which works fine. I'd also like to sort the location objects with an NSSortDescriptor by the date of their latest history item, which as far as I can make out would be "history.@max.time", this however throws the following error: ``` *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Keypath containing KVC aggregate where there shouldn't be one; failed to handle history.@max.time' ``` Halp plox?

Original source