核心数据NSPredicate与to-Many关系

5
我在CoreData中有两个实体,称为User和Coupon,在Many-to-Many关系中。我想获取除了那些由user.userId = 1拥有的所有优惠券以外的所有优惠券,其中userId是NSString。
我使用以下语句作为fetchedResultsController的谓词: [NSPredicate predicateWithFormat:@"NOT(ANY couponOwners.userId = %@)", @"4"]; 但结果未正确过滤。Coupon的couponOwners中的一个User仍然具有userId = 4。
请问是否有人可以帮忙解决?我已经卡了很长时间了。提前致谢。
1个回答

12

"NOT ANY"在Core Data谓词中无法正常工作(这似乎是Core Data的一个错误)。实际上

[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,我在形成正确的谓词方面遇到了问题,我看到您对这个领域非常了解,能否请您帮助解决这个问题?http://stackoverflow.com/questions/37464898/how-to-form-predicate-to-get-objects-from-core-data-that-contain-string-elements?noredirect=1#comment62431649_37464898? - Melany

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接