Im trying to make a predicate for filtering on a many to many relationship. I have a Message object with many Labels as a relationship.
I can do the following:
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"ANY labels.labelId == 4"];
and I will only get the messages that have the Label with the labelId 4. But If I do
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"NONE labels.labelId == 4"];
This will give me every singe Message object I have, even if they have a relationship with a Label with the labelId 4. Why is that? Can someone please help?
It seems that "NONE" or "NOT ANY" does not work as expected in Core Data predicates. Both predicates
NONE labels.labelId == 4
NOT (ANY labels.labelId == 4)
actually return the same result as
ANY labels.labelId != 4
which is not how I understand the documentation. As a workaround, you can use a SUBQUERY:
[NSPredicate predicateWithFormat:@"SUBQUERY(labels, $x, $x.labelId == 4).@count == 0"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With