核心数据NSPredicate与一对多的关系 [英] Core Data NSPredicate with to-Many Relationship

查看:160
本文介绍了核心数据NSPredicate与一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体在CoreData称为用户和优惠券,他们是多对多的关系。我想要获取所有优惠券,除了那些由user.userId = 1,其中userId是NSString拥有。

I have two Entities in CoreData called User and Coupon, they are in Many-to-Many relationship. I wanted to fetch for all Coupons except those owned by user.userId = 1, where userId is NSString.

我使用:
[NSPredicate predicateWithFormat:@NOT(ANY couponOwners.userId =%@) ];
成为我的fetchedResultsController

I used: [NSPredicate predicateWithFormat:@"NOT(ANY couponOwners.userId = %@)", @"4"]; to be the predicate of my fetchedResultsController

的谓词,但不能过滤正确的结果。优惠券优惠券中的其中一个用户仍然具有userId = 4。

but not filtering with correct results. One of the User in couponOwners of the Coupon is still having userId = 4.

有人可以帮忙吗?我被困了一段时间。提前致谢。

Could somebody please help? I have been stuck for quite a while. Thanks in advance.

推荐答案

核心数据谓词与NOT ANY不工作(似乎是一个核心数据错误)。其实

Core Data predicates with "NOT ANY" do not work (that seem to be a Core Data bug). Actually

[NSPredicate predicateWithFormat:@"NOT(ANY couponOwners.userId = %@)", @"4"];

返回与

[NSPredicate predicateWithFormat:@"ANY couponOwners.userId != %@", @"4"];

这当然是错误的。作为解决方法,您可以使用SUBQUERY:

which is of course wrong. As a workaround, you can use a SUBQUERY:

[NSPredicate predicateWithFormat:@"SUBQUERY(couponOwners, $c, $c.userId == %@).@count == 0", @"4"]

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

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