In the following I will describe a simplification of my Core Data schema that I am pretty sure to be equivalent to my real situation.
I have two entity First
and Second
linked by a many-to-many relationship r
.
Second
has got two Boolean attribute, let us call it take
and you_should_not_take
.
I want to make a query that select a row of First
iff it exits an associated row of Second
for with take = true
.
I have tried this predicate:
NSPredicate(format: "ANY (%K == YES && %K == NO)", "r.take","r.you_should_not_take")
but Core Data gives me the following error: "[General] Unable to parse the format string"
.
Maybe I have to use this:
NSPredicate(format: "((ANY %K == YES) && (ANY %K == NO))", "r.take","r.you_should_not_take")
but I am afraid that this last query will select a row of First
iff it exists a row x
of Second
for which x.take == YES
&& it exists a row y
if Second
for which y.you_should_not_take == NO
, with no guarantee that x == y
.
An SQL query would be very simple to be made, but I am not so experienced with Core Data, so while I will try some more queries (and I will test if the second query does what I think it does) I have also asked here hoping the answer would be as simple as in SQL.
To select all First objects which are related to (at least one) Second object for which take==true
and you_should_not_take==false
you have
to use a SUBQUERY. Something like (untested):
SUBQUERY(r, $x, $x.take == YES AND $x.you_should_not_take == NO).@count > 0