核心数据子查询谓词 [英] Core data subquery predicate
问题描述
我有两个实体。
[League]< ---- >> [Game]
游戏有一个属性 kickOffDate
。
我想使用一个谓词来返回所有 Leagues
c $ c>
我使用这个谓词...
// startOfDay和endOfDay是分别返回给定日期的函数,分别为00:00:00和23:59:59
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 andPredicateWithSubpredicate:@ [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建议,您应该验证您的数据确实包含联赛
回答谓词以测试它。
如果仍未得到结果,请检查请求执行期间是否有错误,并且您的数据堆栈已正确初始化。
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):
[NSPredicate predicateWithFormat:@"SUBQUERY(games, $g, $g.kickOffDate >= %@ AND $g.kickOffDate <= %@).@count > 0", [self startOfDay:[NSDate date]],[self endOfDay:[NSDate date]]];
== League that have a game with a kick-off date in a given range
Your solution is equivalent to:
[NSPredicate predicateWithFormat:@"ANY games.kickOffDate >= %@ AND ANY games.kickOffDate <= %@",start,end];
== 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.
这篇关于核心数据子查询谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!