0
votes

I have a PFObject subclass which stores an array of strings as one of its properties. I would like to query for all objects of this class where one or more of these strings start with a provided substring.

An example might help:

I have a Person class which stores a firstName and lastName. I would like to submit a PFQuery that searches for Person objects that match on name. Specifically, a person should be be considered a match if if any ‘component’ of either the first or last name start with the provided search term.

For example, the name "Mary Beth Smith-Jones" should be considered a match for beth and bet, but not eth.

To assist with this, I have a beforeSave trigger for the Person class that breaks down the person's first and last names into separate components (and also lowercases them). This means that my "Mary Beth Smith-Jones" record looks like this:

firstName: “Mary Beth”

lastName: “Smith-Jones”

searchTerms: [“mary”, “beth”, “smith”, “jones”]

The closest I can get is to use whereKey:EqualTo which will actually return matches when run against an array:

    let query = Person.query()
    query?.whereKey(“searchTerms”, equalTo: “beth”)
    query?.findObjectsInBackgroundWithBlock({ (places, error) -> Void in
        //Mary Beth is retuned successfully
    })

However, this only matches on full string equality; query?.whereKey(“searchTerms”, equalTo: “bet”) does not return the record in question.

I suppose I could explode the names and store all possible sequential components as search terms (b,e,t,h,be,et,th,bet,etc,beth, etc) but that is far from scalable.

Any suggestions for pulling these records from Parse? I am open to changing my approach if necessary.

1

1 Answers

0
votes

Have you tried whereKey:hasPrefix: for this? I am not sure if this can be used on array values.

https://parse.com/docs/ios/guide#queries-queries-on-string-values