【问题标题】:CoreData fetch request - complex predicateCoreData 获取请求 - 复杂谓词
【发布时间】:2013-01-06 17:02:34
【问题描述】:

我将如何使用(理想情况下)带有键值编码集合运算符的单个谓词来简化此过程? (最后感兴趣的数组是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 属性roundsRounds,并且Round 有一个NSInteger 属性number,我正在寻找那些Games 的最高编号Round 有一些BOOL 属性someBoolProperty

【问题讨论】:

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


    【解决方案1】:

    这似乎是你想要的:

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

    【讨论】:

    • 谢谢,马丁。不知道子查询,显然除了 NSExpression ref 中的条目之外几乎没有文档
    猜你喜欢
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2021-11-09
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2017-10-13
    相关资源
    最近更新 更多