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

倖福魔咒の 提交于 2020-01-03 07:19:14

问题


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?


回答1:


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



回答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.




回答3:


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


来源:https://stackoverflow.com/questions/3898861/core-data-sorting-by-count-in-a-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!