核心数据与许多关系进行NSPredicate

Joh*_*ohn 5 core-data nspredicate nsfetchedresultscontroller

我在CoreData中有两个名为User and Coupon的实体,它们处于多对多关系中.我想获取所有优惠券,除了user.userId = 1所拥有的优惠券,其中userId是NSString.

我用过: [NSPredicate predicateWithFormat:@"NOT(ANY couponOwners.userId = %@)", @"4"]; 作为fetchedResultsController的谓词

但不能过滤正确的结果.优惠券的couponOwners中的一个用户仍然具有userId = 4.

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

Mar*_*n R 11

带有"NOT ANY"的核心数据谓词不起作用(这似乎是一个核心数据错误).其实

[NSPredicate predicateWithFormat:@"NOT(ANY couponOwners.userId = %@)", @"4"];
Run Code Online (Sandbox Code Playgroud)

返回相同的结果集

[NSPredicate predicateWithFormat:@"ANY couponOwners.userId != %@", @"4"];
Run Code Online (Sandbox Code Playgroud)

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

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