Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data: Many-to-many Predicate NOT not working

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?

like image 698
plaetzchen Avatar asked Dec 20 '22 09:12

plaetzchen


1 Answers

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"]
like image 152
Martin R Avatar answered Dec 28 '22 07:12

Martin R