0

I'm trying to get a subquery predicate to work and I'm struggling.

I have two entities.

[League] <---->> [Game]

Game has a property kickOffDate.

I'd like to use a predicate to return all Leagues that have at least one Game with a kickOffDate today.

I am using this predicate...

// 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]];

However, when I use this I don't seem to get any results.

Have I written the predicate correctly?

Is there a better way of doing this?

4

1 回答 1

5

您是否尝试过(未经测试):

[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回答谓词以对其进行测试。
如果您仍然没有得到任何结果,请检查请求执行期间是否有错误,并且您的数据堆栈是否已正确初始化。

于 2013-08-05T06:53:05.923 回答