我正试图让一个子查询谓词工作,但我很吃力。
我有两个实体。
[联赛]<---->>【游戏】
游戏具有属性kickOffDate
。
我想使用谓词来返回今天至少有一个具有kickOffDate的Game
的所有Leagues
。
我在用这个谓语。。。
// 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]];
然而,当我使用这个时,我似乎没有得到任何结果。
我把谓语写对了吗?
有更好的方法吗?
您尝试过(未经测试)吗:
[NSPredicate predicateWithFormat:@"SUBQUERY(games, $g, $g.kickOffDate >= %@ AND $g.kickOffDate <= %@).@count > 0", [self startOfDay:[NSDate date]],[self endOfDay:[NSDate date]]];
==在给定范围内有开球日期的联赛
您的解决方案相当于:
[NSPredicate predicateWithFormat:@"ANY games.kickOffDate >= %@ AND ANY games.kickOffDate <= %@",start,end];
==在指定日期之后开始比赛,在指定日期之前开始比赛的联赛(可以是不同的比赛,也可以是同一场比赛)
这应该会返回比你喜欢的更多的结果
编辑:
正如@Martin R所建议的,您应该验证您的数据是否确实包括一个League
来回答谓词以测试它。
如果仍然没有得到结果,请检查在执行请求期间是否存在错误,以及数据堆栈是否已正确初始化。