无法想出一个 NSPredicate 来获取我想要的实体?
我有一个包含三个实体的数据模型,如下所示:
Event:
id
startTime
->>invitees
User:
id
type
->>invitations (Invitees entity)
Invitees:
rsvpState
->user
->event
-> =一对一关系
->> = 太多关系
我想做的是创建一个谓词,返回特定类型和 id 的用户已回复“Y”且 startTime 大于现在的所有事件。到目前为止我最接近的是:
SUBQUERY(invitees.user, $x, $x.id == %i AND $x.type == %i).invitations.rsvpState == %@ AND startTime > %@
这不会给我任何错误,尽管我仍然得到奇怪的结果。我想我不完全理解子查询是如何工作的。
例如,在执行 .invitations.rsvpSate == %@ 时,检查所有通过谓词的用户是否都具有 Invitation.rsvpState == “Y”,或者检查结果用户集合以查看其中是否有任何用户有邀请。rsvpState ==“Y”。
通过添加startTime> %@ 到查询末尾是否检查所有结果事件是否具有 startTime > %@ 或结果中的任何事件的 startTime > %@?
谢谢
I have a datamodel with three entities which looks like:
Event:
id
startTime
->>invitees
User:
id
type
->>invitations (Invitees entity)
Invitees:
rsvpState
->user
->event
-> = to-one relationship
->> = too-many relationship
What i am trying to do is create a predicate that returns all the events that a user of a certain type and id has rsvp'd "Y" to and have a startTime greater than now. The closest i have come so far is:
SUBQUERY(invitees.user, $x, $x.id == %i AND $x.type == %i).invitations.rsvpState == %@ AND startTime > %@
This doesn't give me any errors although i am still getting strange results. I guess i dont fully understand how subqueries work.
For example when doing .invitations.rsvpSate == %@ is that checking that ALL users who passes the predicate has an invitation.rsvpState == "Y" or is it checking the resulting collection of users to see if ANY of them have an invitation.rsvpState == "Y".
By adding startTime > %@ to the end of the query does that check if ALL resulting events have a startTime > %@ or that ANY of the events in the results has a startTime > %@?
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许使用 NSCompoundPredicate 的评估器比 SUBQUERY 更容易。它将帮助您拆分查询并更具可读性。
至于你的两个问题,答案总是全部。如果您需要“any”,还有谓词关键字
ANY
。假设
User
的id
是唯一的,则不需要type
属性。另外,我不确定您是否真的需要复合谓词或子查询。请尝试:Maybe it is easier to use
NSCompoundPredicate
s rater thanSUBQUERY
. It would help you split up your query and be more readable.As for your 2 questions, the answer is always ALL. There is also the predicate keyword
ANY
in case you need "any".Assuming that the
id
ofUser
is unique, you do not need thetype
property. Also, I am not sure you actually need a compound predicate or subquery. Please try: