How to use the “ALL” aggregate operation in a NSPredicate to filter a CoreData-based collection

后端 未结 5 2020
滥情空心
滥情空心 2020-12-23 10:39

Based on the data model below

\"dataModel\"

And based on user input I create a NSSet of managedObjects

5条回答
  •  攒了一身酷
    2020-12-23 11:18

    How about using a compound predicate? As I understand you want to return all Entries that match a list of tags not just any of them. One approach would be to create a predicate for each tag, then AND them together using a compound predicate.

    NSMutableArray *predicates = [[NSMutableArray alloc] init];
    for (Tag *tag in selectedTags) {
        [predicates addObject:[NSPredicate predicateWithFormat:@"ANY entryTags.tagName MATCHES %@", tag.tagName]];
    }
    NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
    

    This should achieve want you want. Then just set this predicate on your request.

提交回复
热议问题