无法想出一个 NSPredicate 来获取我想要的实体?

发布于 2024-12-01 00:00:46 字数 751 浏览 0 评论 0原文

我有一个包含三个实体的数据模型,如下所示:

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 > %@

这不会给我任何错误,尽管我仍然得到奇怪的结果。我想我不完全理解子查询是如何工作的。

  1. 例如,在执行 .invitations.rsvpSate == %@ 时,检查所有通过谓词的用户是否都具有 Invitation.rsvpState == “Y”,或者检查结果用户集合以查看其中是否有任何用户有邀请。rsvpState ==“Y”。

  2. 通过添加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.

  1. 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".

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2024-12-08 00:00:46

也许使用 NSCompoundPredicate 的评估器比 SUBQUERY 更容易。它将帮助您拆分查询并更具可读性。

至于你的两个问题,答案总是全部。如果您需要“any”,还有谓词关键字 ANY

假设 Userid 是唯一的,则不需要 type 属性。另外,我不确定您是否真的需要复合谓词或子查询。请尝试:

startTime > %@ AND invitees.user.id == %i AND invitees.rsvpState == %@

Maybe it is easier to use NSCompoundPredicates rater than SUBQUERY. 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 of User is unique, you do not need the type property. Also, I am not sure you actually need a compound predicate or subquery. Please try:

startTime > %@ AND invitees.user.id == %i AND invitees.rsvpState == %@
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文