Reputation: 93
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
Upvotes: 0
Views: 106
Reputation: 80265
Maybe it is easier to use NSCompoundPredicate
s 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 == %@
Upvotes: 2