【问题标题】:Core Data NSPredicate with to-Many Relationship具有多对多关系的核心数据 NSPredicate
【发布时间】:2013-03-30 20:03:15
【问题描述】:

我在 CoreData 中有两个实体,分别称为 User 和 Coupon,它们是多对多关系。我想获取除 user.userId = 1 拥有的所有优惠券之外的所有优惠券,其中 userId 是 NSString。

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

但没有过滤出正确的结果。 Coupon 的 couponOwners 中的一位用户的 userId = 4。

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

【问题讨论】:

    标签: core-data nspredicate nsfetchedresultscontroller


    【解决方案1】:

    带有“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"]
    

    【讨论】:

    • 你真聪明。非常感谢。
    • @Martin R,我在形成正确的谓词时遇到问题,因为我看到你非常了解这个领域,你能帮忙解决这个问题吗? stackoverflow.com/questions/37464898/…?
    猜你喜欢
    • 2011-02-18
    • 2012-03-08
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多