5

次のようなリクエストがあります。

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

これにより、設定した属性を 1 つ以上持つオブジェクトのリストが返されます。渡したすべての属性を持つオブジェクトのリストを取得したいので、試しました:

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

しかし、私は例外を得ました:

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

とにかく、これが正しい要求であるかどうかさえわかりません。属性のリストにあるとしましょう: [red, green, blue]少なくともそれらの属性を持つすべてのオブジェクトを取得するにはどうすればよいですか?

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

属性がないObject_4 (red, green, yellow)からではありません(予想どおり、フェッチリクエストでblue4つのオブジェクトすべてを取得することに注意してください)ANY

編集、関連する質問: 完全一致が必要な場合はどうすればよいですか? だから[red, green, blue]私は得るだけですObject_1か?


編集2:両方の質問に答えることができましたが、新しい質問があります

4

1 に答える 1

6

リストに少なくともすべての属性を持つすべてのオブジェクトをフェッチします

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

リスト内の属性のみを持つすべてのオブジェクトをフェッチします

NSPredicate *objectsWhoseAttributesAreInList =
    [NSPredicate predicateWithFormat:
        @"attributes.@count == %d AND SUBQUERY(attributes, $s, $s.attributes.id IN %@).@count == %d", [attributeIds count], attributeIds, [attributeIds count]];
于 2012-07-07T11:35:35.930 に答える