核心数据子查询谓词

我试图让一个子查询谓词工作,我很挣扎。

我有两个实体。

[联赛] <—- >> [游戏]

游戏有一个属性kickOffDate

我想用一个谓词来返回所有至less有一场Game Leagues ,并且今天有一个kickOffDate。

我正在使用这个谓词

 // startOfDay and endOfDay are functions to return the given date with 00:00:00 and 23:59:59 respectively NSPredicate *startOfDayPredicate = [NSPredicate predicateWithFormat:@"SUBQUERY(games, $g, $g.kickOffDate >= %@).@count > 0", [self startOfDay:[NSDate date]]]; NSPredicate *endOfDayPredicate = [NSPredicate predicateWithFormat:@"SUBQUERY(games, $g, $g.kickOffDate <= %@).@count > 0", [self endOfDay:[NSDate date]]]; NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[startOfDayPredicate, endOfDayPredicate]]; 

但是,当我使用这个我似乎并没有得到任何结果。

我是否正确地编写了谓词?

有没有更好的方法来做到这一点?

你尝试过(未经testing):

 [NSPredicate predicateWithFormat:@"SUBQUERY(games, $g, $g.kickOffDate >= %@ AND $g.kickOffDate <= %@).@count > 0", [self startOfDay:[NSDate date]],[self endOfDay:[NSDate date]]]; 

==在一个给定的范围内有开球date的比赛

您的解决scheme相当于:

 [NSPredicate predicateWithFormat:@"ANY games.kickOffDate >= %@ AND ANY games.kickOffDate <= %@",start,end]; 

==有一个游戏在给定的date之后开始的游戏,并且在给定date之前开始游戏(可以是不同的游戏或相同的游戏)

应该返回比你喜欢的更多的结果。

编辑:
正如@Martin Rbuild议的那样,你应该确认你的数据确实包含了一个League回答谓词来testing它。
如果仍然没有结果,请检查执行请求期间是否有错误,并且数据堆栈已正确初始化。