Core Data: Best Technique to Sort To Many Relationship ordered set

Say I have a NSManagedObject subclass (Parent) with a to many relationship to another object (call the relationship children).

To display children sorted, I could add a method like this to Parent:


-(NSArray*)sortedChildren
{
NSArray *sorted = [self.children sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"key" ascending:YES]]];

    return sorted;
}


It would not be efficient to resort the children every time I need to access sortedChildren. I suppose I could keep a sorted copy in a property and override all the generated methods to add/insert children make sure the sorted array picks up changes....and nil it out in the didBecomeFault method? Is there a better way to do this? There can potentially be a lot of children so I do want to avoid a method like above.

How about something like


if (self.children.hasChanges) {

// resort cached sorted array

}


return cachedSortedArrayProperty;

Do you mean:


if (self.hasChanges)
{
  //resort
}


Or maybe I should do another fetch request?

Core Data: Best Technique to Sort To Many Relationship ordered set
 
 
Q