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?
Have you tried (untested):
== League that have a game with a kick-off date in a given range
Your solution is equivalent to:
== League that have a game that start after a given date and have a game that start before a given date (could be different games, or the same game)
Which should return more results than you like.
Edit:
As @Martin R suggested, you should verify that your data does indeed include a
League
answering the predicate to test it.If you still get no results, check to see if there were errors during execution of the request and your data-stack is properly initialized.