I have difficulties checking if a nsorderedset is a subset of another nsorderedset in a NSPredicate string.
I can achieve the result with the block below:
request.predicate = [NSPredicate predicateWithBlock:^BOOL(id object, NSDictionary *bindings) {
NSOrderedSet *biggerSet = [object valueForKey:@"attributeName"];
return [smallerSet isSubsetOfOrderedSet:biggerSet];
}];
but I believe that in string format could be more performant (am I wrong?).
tried things like @"attribute CONTAINS %@" and @"%@ IN attribute" but with no luck.
Thanks for any help.
Something like this should work:
NS(Ordered)Set *smallerSet = …
request.predicate =
[NSPredicate predicateWithFormat:@"SUBQUERY(attribute, $a, $a IN %@).@count == %lu",
smallerSet, (unsigned long)[smallerSet count]];
The predicate checks if the number of "attribute" values contained in smallerSet
is
equal to the number of elements in smallerSet
. If yes, then smallerSet
must be
a subset of the "attribute" set.
A string based predicate is not generally faster than a block based (compare https://stackoverflow.com/a/21158730/1187415). But a Core Data fetch request (with a SQlite store file) cannot use block based predicates.