Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data, NSPredicate, ANY key.path == nil

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.

enter image description here

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?

like image 442
Amy Worrall Avatar asked Nov 02 '12 10:11

Amy Worrall


1 Answers

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.

like image 140
Dave DeLong Avatar answered Oct 25 '22 09:10

Dave DeLong