Key Data: Sort NSFetchRequest by many-many count

Let's say I have a parent object, each of which has several children. I want all parents to be sorted by 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 to build such a sample using kernel data? If there is no way to do this, sorting using sortedArrayUsingSelector: will lose the benefits of the batch size _PFBatchFaultingArray?

Thanks Ben

+6
objective-c core-data
source share
1 answer

Your request will work, but (assuming the children fail) will use the key-value encoding methods in the children property, which in turn will cause an error (see NSManagedObject docs for a list of methods that lead to failures, and discussion of this behavior), so you will lose performance benefits for batch processing and crashes.

You might want to keep the derived attribute of the parent object (name it childrenCount ), which reflects the number of children associated with the parent, if possible for your situation. This is not a clean solution, but if you save it as NSNumber in the parent object that you will have access to, even if the children error is resolved, and you can sort it directly.

+5
source share

All Articles