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

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

问题描述

我在 CoreData 中有两个实体,称为 User 和 Coupon,它们处于多对多关系.我想获取所有优惠券,除了 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(ANYcouponOwners.userId = %@)",@"4"];成为我的 fetchedResultsController 的谓词

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

但没有过滤出正确的结果.Coupon 的couponOwners 中的用户之一仍然具有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 谓词不起作用(这似乎是一个 Core Data 错误).其实

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"];

这当然是错误的.作为一种解决方法,您可以使用子查询:

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天全站免登陆