NSPredicate带有!=? [英] NSPredicate with a !=?

查看:330
本文介绍了NSPredicate带有!=?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有核心数据实体人员和边界.他们之间存在多对多关系(每个人可以有很多边界,每个边界可以有很多人).

I have Core Data Entities Person and Boundary. They have a many-to-many relationship (each person can have many boundaries, and each boundary can have many persons).

我正在尝试创建一个列表,列出Fred Fred也没有任何关系.

I am trying to create a list of what boundaries Person Fred doesn't have a relationship too.

Person *person = [Person MR_findFirstByAttribute:@"name" withValue:@"Fred"];
DLog(@"person.boundaries.count: %d", person.boundaries.count);

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY persons != %@", person];

DLog(@"testBoundaries.count: %d", [Boundary MR_countOfEntitiesWithPredicate:predicate]);

我在数据库中有47个边界,Fred可以看到所有47个边界.因此,我希望我的提取操作将返回0:

I have 47 boundaries in the database, and Fred can see all 47. So I expect my fetch to return 0:

DEBUG | -[LoginViewController viewDidLoad] | person.boundaries.count: 47
DEBUG | -[LoginViewController viewDidLoad] | testBoundaries.count: 47

我的谓词怎么了?

推荐答案

[NSPredicate predicateWithFormat:@"ANY persons != %@", fred]

查找与Fred以外的任何其他人相关的所有对象.您想要的是

finds all objects that are related to any person other that Fred. What you want is

[NSPredicate predicateWithFormat:@"NOT(ANY persons = %@)", fred]

,并且此应该返回与Fred无关的所有对象.

and this should return all objects that are not related to Fred.

但是,似乎有一个核心数据错误,"NOT ANY"或"NONE"无法正常工作 在谓词中 比较 NSPredicate聚合运算与无.解决方法是使用SUBQUERY:

However, there seems to be a Core Data bug that "NOT ANY" or "NONE" do not work correctly in a predicate, compare NSPredicate Aggregate Operations with NONE. The workaround is to use a SUBQUERY:

[NSPredicate predicateWithFormat:@"SUBQUERY(persons, $p, $p == %@).@count == 0", fred]

这篇关于NSPredicate带有!=?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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