Basic data: the many-to-many predicate does NOT work

I am trying to create a predicate to filter on many relationships. I have a Message object with many shortcuts as a relation.

I can do the following:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"ANY labels.labelId == 4"]; 

and I will only get messages tagged with tag 4. If I do

 fetchRequest.predicate = [NSPredicate predicateWithFormat:@"NONE labels.labelId == 4"]; 

This will give me every single message object that I have, even if they have a link to the label with labelId 4. Why? Can anybody help?

+4
source share
1 answer

It seems that β€œNO” or β€œNOT ANY” does not work properly in Core Data predicates. Both predicates

  NONE labels.labelId == 4 NOT (ANY labels.labelId == 4) 

really returns the same result as

  ANY labels.labelId != 4 

which is not how I understand the documentation. As a workaround, you can use SUBQUERY:

 [NSPredicate predicateWithFormat:@"SUBQUERY(labels, $x, $x.labelId == 4) .@count == 0"] 
+11
source

All Articles