核心数据,NSPredicate,任何 key.path == nil [英] Core Data, NSPredicate, ANY key.path == nil

查看:26
本文介绍了核心数据,NSPredicate,任何 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.

作为谓词,我可以执行以下操作:

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.

但是,以下确实有效:

[NSPredicate predicateWithFormat:@"SUBQUERY(conditions, $x, $x.terrain == nil).@count > 0"];

谁能解释为什么在搜索 nil 时我不能使用 ANY 语法?

Can anyone explain why, when searching for nil, I can't use the ANY syntax?

推荐答案

谁能解释为什么在搜索 nil 时我不能使用 ANY 语法?

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

这将通过获取 SELF 对象(一个 Advice 实例)并请求 valueForKeyPath:@"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"];

因此,您拥有一组(潜在的)Terrain 实例.现在,我们对 Objective-C 中的集合了解多少?一方面,它们不能包含 nil.它们只能包含对象.这意味着当它执行谓词的 ANY 部分时,它将遍历数组中的项目,并看到它们都不是 nil.

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.

因此,您的谓词失败了.我尝试使用其他一些变体(使用 [NSNull null] 而不是 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.

因此,您使用 SUBQUERY 来解决这个问题似乎与您所能获得的一样好.我强烈建议您提交错误,详细说明您的期望并说明您认为这可行的原因.

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.

这篇关于核心数据,NSPredicate,任何 key.path == nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆