Reputation: 46598
How to use fetched property to create sorted array? I am new to predicate programming.
Say there are two entities: 'MyEntity' and 'Image'. 'MyEntity' have a to-many relationship to 'Image' called 'images'. 'Image' have two attributes: 'image' and 'index' where I want to use 'index' as sort key to sort images.
I want to have a fetched property 'sortedImages' on 'MyEntity' which will return a sorted version of 'images'.
What I used to do is load all of the images and sort them in memory and store the result but I found this may consume too much memory.
- (NSArray *)sortedImages {
if (!sortedImages) {
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES];
sortedImages = [self.images sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
[sortedImages retain];
}
return sortedImages;
}
Upvotes: 0
Views: 694
Reputation: 1783
Not sure if a fetchedProperty is the right solution here, but you can create an NSFetchRequest on the managedObjectContext of the parent entity and use an NSSortDescriptor on the key, "index". That should return a "fault-capable" sorted array of image objects.
Upvotes: 1