I came up with a solution to this using subquery, but I don’t understand why what I was trying to do first didn’t work.
Here’s my data model. I’m fetching on Advice.

I can do the following as a predicate:
[NSPredicate predicateWithFormat:@"ANY conditions.terrain == %@", aTerrainObject];
that works fine, and returns any piece of advice where at least one of its conditions has that terrain type.
However, when I try to do this, it fails:
[NSPredicate predicateWithFormat:@"ANY conditions.terrain == nil"];
What I want to do is return any piece of advice where at least one of its conditions doesn’t have a terrain type set.
However, the following does work:
[NSPredicate predicateWithFormat:@"SUBQUERY(conditions, $x, $x.terrain == nil).@count > 0"];
Can anyone explain why, when searching for nil, I can’t use the ANY syntax?
Yep! Here’s what’s going on.
First, let’s break this up into the appropriate left and right expressions:
This will be evaluated by taking the
SELFobject (anAdviceinstance) and requesting thevalueForKeyPath:@"conditions.terrain". The result of this keypath will be a collection. You’re essentially doing:So, you have a collection of (potential)
Terraininstances. Now, what do we know about collections in Objective-C? Well for one thing, they cannot containnil. They can only contain objects. This means that when it executes theANYportion of the predicate, it’s going to iterate through the items in the array and see that none of them arenil.Thus, your predicate is failing. I tried playing around with some other variations (using
[NSNull null]instead ofnil, etc), but none of them seemed to work.It would therefore appear that your use of a
SUBQUERYto solve this is about as good as you could get. I would highly recommend filing a bug detailing your expectations and expressing why you think this should work.