avenged
avenged

Reputation: 547

Core Data: sorting by count in a to-many relationship

I am currently trying to setup a NSFetchedResultsController that will order my table view based on the number of entities in a to-many relationship. I am not sure if this makes a difference when counting, but this is also an inverse relationship.

I thought something like this would work just fine:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Parent"
inManagedObjectContext:managedObjectContext];

NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] 
initWithKey:@"children.@count" ascending:YES];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,
nil];

[fetchRequest setSortDescriptors:sortDescriptors];

I keep getting 'Keypath containing KVC aggregate where there shouldn't be one; failed to handle children.@count'.

Any ideas on what could be going wrong?

Upvotes: 13

Views: 3817

Answers (4)

numist
numist

Reputation: 627

Since iOS 13 (and friends) you can create a derived attribute called childCount with the derivation expression children.@count and then set fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"childCount" ascending:NO]]

Upvotes: 5

Bram De Geyter
Bram De Geyter

Reputation: 1098

I added the KVO accessor -countOf<Key> as an attribute to my managed object model as an integer type. I did NOT implement anything for this attribute in my NSManagedObject subclass, as all the magic seems to happen under the hood.

So in this particular case, add an attribute countOfChildren to the Parent entity. This should remove the exception.

EDIT: This fix seems to only work on iOS 6.1, not on iOS 6.0.

Upvotes: 2

jacob bullock
jacob bullock

Reputation: 651

AS far as I know you can't apply the @count in the query, but you can easily apply it to the fetched array.

NSEntityDescription * entity = [NSEntityDescription entityForName:@"Parent" inManagedObjectContext:self.managedObjectContext];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSError *error;
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"children.@count" ascending:NO];
NSArray *descriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
NSArray *sortedArray  = [results sortedArrayUsingDescriptors:descriptors];

Upvotes: 3

Nik Burns
Nik Burns

Reputation: 3423

i ended up adding a count property into my entity, I just increased it whenever a new relationship was added. made the fetchedResultsController easier to use.

Be interested if you found a way using KVC. I never needed to fix this hack.

something like this may be useful:

entity *match;    
match = (entity *)[objects objectAtIndex:0];
        NSSet *t = [match valueForKey:@"entity"]; 
        if(![t containsObject:newEntity]){
            int newCount = [match.count intValue] +1;
        [match addEntityObject:newEntity];
            [match setCount:[NSNumber numberWithInt:newCount]];

Upvotes: -1

Related Questions