Core data 核心数据预测与多对多关系

Core data 核心数据预测与多对多关系,core-data,nspredicate,nsfetchedresultscontroller,Core Data,Nspredicate,Nsfetchedresultscontroller,我在CoreData中有两个实体,叫做用户和优惠券,它们是多对多关系。我想获取所有优惠券,但user.userId=1拥有的优惠券除外,其中userId是NSString 我用过: [NSPredicate predicateWithFormat:@“不是(任何couponOwners.userId=%@)”,@“4” 作为我的fetchedResultsController的谓词 但是没有过滤出正确的结果。优惠券的couponOwners中的一个用户的userId=4 有人能帮忙吗?我被困了好

我在CoreData中有两个实体,叫做用户和优惠券,它们是多对多关系。我想获取所有优惠券,但user.userId=1拥有的优惠券除外,其中userId是NSString

我用过:
[NSPredicate predicateWithFormat:@“不是(任何couponOwners.userId=%@)”,@“4”
作为我的fetchedResultsController的谓词

但是没有过滤出正确的结果。优惠券的couponOwners中的一个用户的userId=4

有人能帮忙吗?我被困了好一阵子了。提前谢谢

带有“notany”的核心数据谓词不起作用(这似乎是一个核心数据bug)。实际上

[NSPredicate predicateWithFormat:@"NOT(ANY couponOwners.userId = %@)", @"4"];
返回与相同的结果集

[NSPredicate predicateWithFormat:@"ANY couponOwners.userId != %@", @"4"];
这当然是错误的。作为解决方法,您可以使用子查询:

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

你真聪明。非常感谢。@Martin R,我在形成一个正确的谓词方面有一个问题,因为我看到你非常了解这个领域,你能帮我解决这个问题吗?