CoreData获取请求 - 复杂谓词

时间:2013-01-06 17:02:35

标签: objective-c ios core-data nspredicate key-value-coding

如何使用(理想情况下)使用键值编码收集运算符的单个谓词来简化此操作? (最后感兴趣的数组是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];
}

游戏的NSSet属性roundsRoundsRoundNSInteger属性number,我正在寻找最高编号Games Round属性BOOL的{​​{1}}人。

1 个答案:

答案 0 :(得分:2)

这似乎可以做你想要的:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Game"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SUBQUERY(rounds, $r, $r.someBoolProperty = YES).number = @max.rounds.number"];