views:

577

answers:

2

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 "[email protected]", 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 [email protected]'

Halp plox?

A: 

Probably not the best solution, but you could always pull back the data and sort it after the fact. Have a look at Sorting and Filtering NSArray Objects.

slf
A: 

Here is some code that I have where I do similar

NSError *error;

if (![[self fetchedResultsController] performFetch:&error])
{
 UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
 [alert show];
}
else
{
 self.feedsArray = [fetchedResultsController fetchedObjects];

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"[email protected]" ascending:NO];
 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

 self.feedsArray = [feedsArray sortedArrayUsingDescriptors:sortDescriptors];

 [sortDescriptors release];
 [sortDescriptor release];

 NSInteger overviewAmount = [[feedsArray valueForKeyPath:@"@sum.unreadArticles"] integerValue];

    ....
}
kdbdallas
Any idea what might've fixed it though? Can't really see anything in that code that could've been the fix...
Joonas Trussmann
I am not sure exactly... Do you have your Entity sublcassed? Both of mine are (so in the example above the entity I am doing the fetch on and the articles entity are subclassed) I do not have anything special in the subclass for articles, so I do not really remember why I subclassed it, so perhaps this was why... Another thought is in my articles Entity the attribute postDate which I do the @max on I have setup in the modal to be indexed. Perhaps try that. Also make sure your date value has a default value. I use: 1980-01-01 00:00:01
kdbdallas