Search code examples
iosobjective-cnspredicatempmediaquery

how to create a set of MPMediaPropertyPredicate joined by OR rather than AND


I would like to add a series of predicates to an MPMediaQuery, but I want it aggregated using OR, not AND:

MPMediaQuery *q = [MPMediaQuery songsQuery];
NSMutableArray *a = [@[] mutableCopy];
for (DFLocalMediaItem *item in self.offlineItems) {
    MPMediaPropertyPredicate* p = [MPMediaPropertyPredicate predicateWithValue:item.identifier
                                                                           forProperty:MPMediaItemPropertyPersistentID];
    [a addObject:p];
}
NSSet *s = [NSSet setWithArray:a];
[q setFilterPredicates:s];

when I create this set, it's aggregating the above MPMediaPropertyPredicate using AND (which won't work obviously.. since a single MPMediaItem will obviously have a single identifier)

ideas?


Solution

  • You couldn't have such functionality due to public API. The official docs says that

    When you apply more than one predicate to a query, the query combines them using the logical AND operator.

    But you could construct an MPMediaItemCollection if you want from your items or you could construct from an array similar to MPMediaQuery structure that has the same methods, but has static array behind.