Core Data NSPredicate with to-Many Relationship

Core Data predicates with “NOT ANY” do not work (that seem to be a Core Data bug). Actually

[NSPredicate predicateWithFormat:@"NOT(ANY couponOwners.userId = %@)", @"4"];

returns the same result set as

[NSPredicate predicateWithFormat:@"ANY couponOwners.userId != %@", @"4"];

which is of course wrong. As a workaround, you can use a SUBQUERY:

[NSPredicate predicateWithFormat:@"SUBQUERY(couponOwners, $c, $c.userId == %@).@count == 0", @"4"]

Leave a Comment