2013-01-06 117 views
1

我將如何使用(理想情況下)使用鍵值編碼集合運算符的單個謂詞來簡化此操作? (感興趣到底該數組是filteredGamesCoreData獲取請求 - 複雜謂詞

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,並RoundNSInteger財產number,和我在尋找那些Games,其最高數Round有一些BOOL財產someBoolProperty

回答

2

這似乎做你想要什麼:

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

感謝,馬丁。不知道子查詢,顯然除了NSExpression參考文獻中的條目外,幾乎沒有文檔 –