Core Data ANY BETWEEN predicate

做~自己de王妃 提交于 2019-12-03 13:49:28

问题


I'm trying to create an NSPredicate to find 'projects' that contain 'sessions' within a certain date range. I tried this at first:

[NSPredicate predicateWithFormat:@"ANY sessions.date BETWEEN {$STARTDATE, $ENDDATE}"];

But I get an exception:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
   'to-many key not allowed here'

It seems BETWEEN does not work with ANY in this way. I'm also limited in use of () and AND clauses meaning I can't use something like:

[NSPredicate predicateWithFormat:@"ANY (sessions.date > $STARTDATE && sessions.date < $ENDDATE)"];

If I try that I get a parse error. And of course sessions.date is really a set so ANDing it like that doesn't really make a lot of sense.

How can I do this?

Thanks

UPDATE: Note that this:

[NSPredicate predicateWithFormat:@"ANY sessions.date > $STARTDATE && ANY sessions.date < $ENDDATE"];

Is incorrect because it returns a project where there is a session greater than the start date and another session less than the end date but no session in between.


回答1:


I think this is a case where you need a SUBQUERY expression:

[NSPredicate predicateWithFormat:@"SUBQUERY(sessions, $s, $s.date BETWEEN {$STARTDATE, $ENDDATE}).@count > 0"];


来源:https://stackoverflow.com/questions/2630633/core-data-any-between-predicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!