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

后端 未结 2 1636
抹茶落季
抹茶落季 2020-12-16 20:48

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 Adv

相关标签:
2条回答
  • 2020-12-16 20:50

    I believe you cannot use nil in the query. You should maybe use NULL or the format string construct like "ANY conditions.terrain == %@", nil.

    What puzzles me is that your subquery is working...

    The above is not correct, as NULLand NILcan be used interchangeably.

    Instead, the NSExpression Class Reference gives exactly your pattern (with @count) as the preferred example.

    Did you check that the terrain relationship is optional?

    0 讨论(0)
  • 2020-12-16 21:06

    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.

    0 讨论(0)
提交回复
热议问题