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 f开发者_StackOverflow社区etch like this using core data? If there is no way to do this will sorting using sortedArrayUsingSelector:
loose the benefits of _PFBatchFaultingArray batch size?
Thanks, Ben
Your query would work, but (assuming children
is faulted) would use key-value coding methods on the children
property, which in turn would fire the fault (see the NSManagedObject docs for a list of methods that fire faults, and a discussion of this behavior), so you'd lose the performance benefits of batching and faulting.
You might consider maintaining a derived attribute on your parent entity (call it childrenCount
) that reflects the number of children related to the parent, if this is feasible for your situation. It's not the cleanest solution, but if you keep it as an NSNumber in the parent entity you'd have access to it even if children
is faulted, and you can sort on it directly.
精彩评论