Search code examples
swiftcore-datapredicatensfetchrequest

CoreData Predicate get every sentence that contains any word in array


In Swift 4 I have a CoreData "Sentence" model that has a String attribute "englishsentence". I also have an array of "words" and would like to fetch all sentences for which the "englishsentence" attribute contains one or more of the words in the array.

var words = ["today", "yesterday", "tomorrow"]

This array is just an example. It is supposed to change at runtime and can have any length.

and in the fetch request I am trying to do something like this:

let fetchRequest =
    NSFetchRequest<NSManagedObject>(entityName: "Sentence")
let predicate = NSPredicate(format: "ANY englishsentence CONTAINS ANY word IN %@", words)
    fetchRequest.predicate = predicate

I am able to create a predicate for all sentences that contain one particular word. However, I cannot get it to work with an array of words unless of course I iterate through the words-array and make a new fetch request for every single word. But this seems awfully inefficient.


Solution

  • One option would be to create a “compound predicate:”

    let words = ["today", "yesterday", "tomorrow"]
    
    let predicates = words.map {
        NSPredicate(format: "englishsentence CONTAINS %@", $0)
    }
    let predicate = NSCompoundPredicate(orPredicateWithSubpredicates: predicates)
    

    Another option is to match against a regular expression:

    let regex = ".*(" + words.map {
        NSRegularExpression.escapedPattern(for: $0)
    }.joined(separator: "|") + ").*"
    
    let predicate = NSPredicate(format: "englishsentence MATCHES %@", regex)
    

    To match only “whole words” you can add the word boundary pattern \b:

    let regex = ".*\\b(" + words.map {
        NSRegularExpression.escapedPattern(for: $0)
    }.joined(separator: "|") + ")\\b.*"