如何使用“全部”聚合操作在NSPredicate中过滤基于CoreData的集合 [英] How to use the "ALL" aggregate operation in a NSPredicate to filter a CoreData-based collection

查看:152
本文介绍了如何使用“全部”聚合操作在NSPredicate中过滤基于CoreData的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于下面的数据模型





根据用户输入,我创建一个名为 selectedTags 的实体标签的managedObjects的NSSet。






我的问题:



  [NSPredicate predicateWithFormat:@ANY entryTags IN%@,selectedTags]; 

...这将返回任何条目,至少有一个entryTag是在selectedTags集中。



我想要的东西如下:

  [NSPredicate predicateWithFormat:@ALL entryTags IN%@,selectedTags]; 

...注意,唯一的变化是ANY到ALL。这说明了我想要什么,但不工作。



要制定我期望的结果:



我在寻找一个解决方案, entryTag都在selectedTags列表中(但如果可能,则不一定是其他方式)。



要进一步说明:



(标签)妈

(标签)爸爸

(标签)礼品



(条目)她是一个她.....(标签)妈

(条目)他是一个他........(标签)爸爸

(标签:)爸爸,礼物


$ b(进入) $ b

如果selectedTags包含mom和gifts,则将显示条目gifts for dad,因为它有标签gifts。我希望它不显示:)

解决方案

到目前为止这是明确的答案:



[NSPredicate predicateWithFormat:@SUBQUERY(entryTags,$ tag,$ tag IN%@)。@ count =%d,selectedTags,[selectedTags count] ];

BEAUTIFUL。



感谢Dave DeLong 。


Based on the data model below

And based on user input I create a NSSet of managedObjects of entity Tag called selectedTags.


My problem:

[NSPredicate predicateWithFormat:@"ANY entryTags IN %@", selectedTags];

... this will return any Entry with at least one entryTag that is in the selectedTags set.

I want something along the lines of:

[NSPredicate predicateWithFormat:@"ALL entryTags IN %@", selectedTags];

... notice the only change is the "ANY" to "ALL". This illustrates what I want, but does not work.

To formulate the outcome I expect:

I'm looking for a solution that will return only Entries who's entryTags are all in the selectedTags list (but at the same time, if possible, not necessarily the other way around).

To further illustrate:

(tag)Mom
(tag)Dad
(tag)Gifts

(entry)she is a she.....(tag)mom
(entry)he is a he........(tag)dad
(entry)gifts for mom...(tags:)mom, gifts
(entry)gifts for dad.....(tags:)dad, gifts

If selectedTags contains "mom" and "gifts", then the entry "gifts for dad" will show up, since it has the tag "gifts". I'd rather have it not show :)

解决方案

This is the definite answer so far:

[NSPredicate predicateWithFormat:@"SUBQUERY(entryTags, $tag, $tag IN %@).@count = %d", selectedTags, [selectedTags count]];

B-E-A-U-T-I-F-U-L.

Thanks to Dave DeLong.

这篇关于如何使用“全部”聚合操作在NSPredicate中过滤基于CoreData的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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