核心数据:多对多谓词不起作用 [英] Core Data: Many-to-many Predicate NOT not working

查看:121
本文介绍了核心数据:多对多谓词不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个谓词来过滤多对多的关系。我有一个带有许多标签的Message对象作为关系。

Im trying to make a predicate for filtering on a many to many relationship. I have a Message object with many Labels as a relationship.

我可以执行以下操作:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"ANY labels.labelId == 4"];

我只会获得带有labelId 4的Label的消息。但是,如果我这样做

and I will only get the messages that have the Label with the labelId 4. But If I do

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"NONE labels.labelId == 4"];

这将给我每个单一的Message对象,即使它们与Label有关系labelId 4.为什么?有人可以帮忙吗?

This will give me every singe Message object I have, even if they have a relationship with a Label with the labelId 4. Why is that? Can someone please help?

推荐答案

似乎无或没有任何在Core Data谓词中无法正常工作。两个谓词

It seems that "NONE" or "NOT ANY" does not work as expected in Core Data predicates. Both predicates

 NONE labels.labelId == 4
 NOT (ANY labels.labelId == 4)

实际上返回与

 ANY labels.labelId != 4

这不是我对文档的理解。作为解决方法,您可以使用SUBQUERY:

which is not how I understand the documentation. As a workaround, you can use a SUBQUERY:

[NSPredicate predicateWithFormat:@"SUBQUERY(labels, $x, $x.labelId == 4).@count == 0"]

这篇关于核心数据:多对多谓词不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆