Core Data NSPredicate with to-Many Relationship

后端 未结 1 1079
花落未央
花落未央 2020-12-06 03:37

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

相关标签:
1条回答
  • 2020-12-06 04:09

    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"]
    
    0 讨论(0)
提交回复
热议问题