I have CoreData object "Entity" with @property (nonatomic, retain) NSSet *ImagesSet;
Inside ImagesSet are many "ImageEntity" objects.
"ImageEntity" have @property (nonatomic, retain) NSNumber * sortKey;
How create fetchRequest for "Entity" with imagesSet ordered by sortKey field?
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity"
inManagedObjectContext:[[DataManager sharedInstance] mainObjectContext]];
[fetchRequest setEntity:entity];
NSString * predicate = some predicate string......;
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:predicate]];
[fetchRequest setFetchBatchSize:2500];
[fetchRequest setFetchLimit:25];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"ImageEntity.sortKey" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *frc = nil;
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:mainObjectContext
sectionNameKeyPath:nil
cacheName:cashName];
Program crashes with message "***** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'**"
sortDescriptor, does it works? I suspect that the real issue could be your predicate. - Larmegroup byand aggregate to get a single value to sort by. WithNSPredicateandNSSortDescriptorthere's no way to specify this kind of logic. - Avi