13

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?

avenged
  • 567
  • 1
  • 8
  • 19
  • You may want to use `arrayWithObject:` instead of `alloc` and `initWithObjects:`. Among other reasons, not releasing the array will not cause a leak. – Peter Hosey Oct 10 '10 at 04:10
  • This was just pseudo code for what I am trying to accomplish, I am release the array in my actual code. – avenged Oct 10 '10 at 04:41
  • Similar question: http://stackoverflow.com/questions/1520522/nssortdescriptor-and-to-many-relationships – paulmelnikow Aug 17 '11 at 06:16

4 Answers4

5

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]]

numist
  • 517
  • 5
  • 7
3

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];
jacob bullock
  • 651
  • 4
  • 14
2

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.

Bram De Geyter
  • 1,088
  • 8
  • 15
  • 1
    hey, could you possibly give more detail on this? the author should have added countOfChildren attribute in the model, right? what type would of attribute would it be? is there more to this setup? – Sergey Katranuk Jul 01 '14 at 12:28
-1

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]];
Nik Burns
  • 3,363
  • 2
  • 29
  • 37