Reputation: 694
How would I simplify this using (ideally) a single predicate with key-value coding collection operators? (The array of interest in the end is filteredGames
.)
NSManagedObjectContext *context;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Game" inManagedObjectContext:context];
NSArray *games = [context executeFetchRequest:request error:nil];
NSMutableArray *filteredGames = [[NSMutableArray alloc] init];
for (Game *game in games) {
NSInteger maxRoundNumber = [game.rounds valueForKeyPath:@"@max.number"];
Round *maxRound = [[game.rounds filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"number = %d", maxRoundNumber]] anyObject];
if (maxRound.someBoolProperty)
[filteredGames addObject:game];
}
Game has an NSSet
property rounds
of Rounds
, and Round
has an NSInteger
property number
, and I'm looking for those Games
whose highest-number Round
has some BOOL
property someBoolProperty
.
Upvotes: 1
Views: 510
Reputation: 539685
This seems to do what you want:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Game"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SUBQUERY(rounds, $r, $r.someBoolProperty = YES).number = @max.rounds.number"];
Upvotes: 2