Core Data NSPredicate with to-Many Relationship

北城余情 提交于 2019-11-27 04:35:14

问题


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.

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

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.


回答1:


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

returns the same result set as

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


来源:https://stackoverflow.com/questions/15722930/core-data-nspredicate-with-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!