核心数据不支持的谓词与ALL和IN [英] Core Data unsupported predicate with ALL and IN

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

问题描述

我有这样的请求:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY attributes.attribute.attributeId IN %@", attributeIds];

这将返回一个对象列表,其中包含一个或多个属性。我想获得一个具有所有属性的对象列表,所以我试过:

That will return a list of objects, that have one ore more of the attributes I set. I want to get a list of objects that have all the attributes I pass, so I tried :

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL attributes.attribute.attributeId IN %@", attributeIds];

但我有一个例外:

'NSInvalidArgumentException', reason: 'Unsupported predicate (null)'

无论如何,我甚至不确定这是正确的请求。假设我有属性列表: [red,green,blue] 如何获得至少有这些属性的所有对象?

Anyway, I'm not even sure that this is the right request. Say that I have at list of attributes : [red, green, blue], How can I get all objects that have at least those attributes ?

* Object_1 (red, green, blue)
* Object_2 (red, green, blue, yellow, brown)
* Object_3 (red, green blue, black, brown)

$ c> Object_4(red,green,yellow),因为它没有 blue 属性 ANY 获取请求,如预期)

but not Object_4 (red, green, yellow) because it doesn't have the blue attribute (note that I get all 4 objects with my ANY fetch request, as expected)

edit,相关问题: ?因此 [red,green,blue] 我只会得到 Object_1

edit, related question : what if I want a full match ? So for [red, green, blue] I will only get Object_1 ?

编辑2:我设法回答了这两个问题,但我有一个新的

edit 2 : I managed to answer both questions, but I have a new one

推荐答案

获取所有至少具有列表中所有属性的所有对象



Fetch all Objects that have at least all attributes in list

NSPredicate *objectsThatContainsAtLeastAllAttributesInList =
    [NSPredicate predicateWithFormat:
        @"SUBQUERY(attributes, $s, $s.attribute.attributeId IN %@).@count == %d", attributeIds, [attributeIds count]];    



获取所有只有列表中的属性的对象



Fetch all Objects that have only the attributes in list

NSPredicate *objectsWhoseAttributesAreInList =
    [NSPredicate predicateWithFormat:
        @"attributes.@count == %d AND SUBQUERY(attributes, $s, $s.attributes.id IN %@).@count == %d", [attributeIds count], attributeIds, [attributeIds count]];

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

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