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?
Can anyone explain why, when searching for nil, I can't use the ANY syntax?
Yep! Here's what's going on.
[NSPredicate predicateWithFormat:@"ANY conditions.terrain == nil"];
First, let's break this up into the appropriate left and right expressions:
conditions.terrain
This will be evaluated by taking the SELF
object (an Advice
instance) and requesting the valueForKeyPath:@"conditions.terrain"
. The result of this keypath will be a collection. You're essentially doing:
Advice *a = ...;
NSSet *conditions = [a conditions];
NSSet *terrains = [conditions valueForKey:@"terrain"];
So, you have a collection of (potential) Terrain
instances. Now, what do we know about collections in Objective-C? Well for one thing, they cannot contain nil
. They can only contain objects. This means that when it executes the ANY
portion of the predicate, it's going to iterate through the items in the array and see that none of them are nil
.
Thus, your predicate is failing. I tried playing around with some other variations (using [NSNull null]
instead of nil
, etc), but none of them seemed to work.
It would therefore appear that your use of a SUBQUERY
to 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.
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