Core Data: NSFetchRequest sorting by count of to-many relationship
- by Ben Reeves
Say I have an parent entity, each of which have a number of children. I want to get all the parents sorted by their number of children. Something similar to the following pseudo code:
NSEntityDescription * entity = [NSEntityDescription entityForName:@"Parent" inManagedObjectContext:managedObjectContext];
[[NSSortDescriptor alloc] initWithKey:@"children.count" ascending:NO];
//Execute request
Is there a way construct a fetch like this using core data?
Thanks,
Ben