首页 文章

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

提问于
浏览
5

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

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

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

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

1 回答

  • 11

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

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

    返回相同的结果集

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

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

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

相关问题