How to fetch entities with several conditions of a to-many relationship met?

How do I fetch entities with a number of different conditions met in a to-many relationship? In other words, if I have an entity "Recipient" which has a to-many relationship with entity Group, how to I fetch all recipients that are in Group A and B but not in Group C?

Answered by NotMyName in 268854022

The technical answer to your question might be, depending on how complicated your conditions are, "You use a sub-query."


Subqueries can be somewhat complicated, and the Apple documentation tends to be light on how to compose and use them. The explanation that got me started was a post "What the heck is SUBQUERY?"


funwithobjc.tumblr.com/post/2726166818/what-the-heck-is-subquery


(not posted as a link because of the forum moderation software).


---

On the other hand, if your conditions are simple, then you just have to join your conditions using the appropriate || or && clauses. For example, if you're determining group membership using a to-many relationship groups and you have the three different groups as groupAReference, groupBReference, and groupCReference, then that expression is just

((%@ in groups) || (%@ in groups)) && !(%@ in groups)

and then just supply groupAReference, groupBReference, groupCReference as the parameters for call to predicateWithFormat.

Accepted Answer

The technical answer to your question might be, depending on how complicated your conditions are, "You use a sub-query."


Subqueries can be somewhat complicated, and the Apple documentation tends to be light on how to compose and use them. The explanation that got me started was a post "What the heck is SUBQUERY?"


funwithobjc.tumblr.com/post/2726166818/what-the-heck-is-subquery


(not posted as a link because of the forum moderation software).


---

On the other hand, if your conditions are simple, then you just have to join your conditions using the appropriate || or && clauses. For example, if you're determining group membership using a to-many relationship groups and you have the three different groups as groupAReference, groupBReference, and groupCReference, then that expression is just

((%@ in groups) || (%@ in groups)) && !(%@ in groups)

and then just supply groupAReference, groupBReference, groupCReference as the parameters for call to predicateWithFormat.

How to fetch entities with several conditions of a to-many relationship met?
 
 
Q