Home > Software engineering >  Parse-Swift Check multiple columns (Compound / OR statement)
Parse-Swift Check multiple columns (Compound / OR statement)

Time:02-26

I have a table where I have three columns in my database that can contain a String, I need to check all three and return that row of data if ANY of those columns have what I'm looking for.

For example the string I'm looking for might appear in the first column (firstArtist) for one row but could appear in the second column (secondArtist) and possibly in the third column (thirdArtist) for another.

I'm using Parse-Swift not Parse.

Wallpaper is my model class.

I've tried something like this:

var testString = "example"

let query = Wallpaper.query()
            .where(Wallpaper.CodingKeys.firstArtist.rawValue == testString)
            .where(Wallpaper.CodingKeys.secondArtist.rawValue == testString)
            .where(Wallpaper.CodingKeys.thirdArtist.rawValue == testString)

But nothing gets retrieved from the database, I assume this is because since it doesn't find anything with the first where statement it doesn't include those results in the rest which could contain the string.

The following works:

let query = Wallpaper.query()

       query.findAll { result in
            switch result {
            case let .success(wallpapers):
                var test: [Wallpaper] = []
                for item in wallpapers {
                    if item.artists.contains(testString) {
                        test.append(item)
                    }
                }
                completion(.success(test))
            case let .failure(error):
                completion(.failure(error))
            }
        }

But the looping through the results and manually filtering them causes my app to take more that ten seconds to load the results, since I'm grabbing everything which isn't ideal.

How can I do the equivalent of a OR statement / Compound statement within Parse-Swift? I've looking through the documentation but I cant find and examples of this.

I found this repository Github Parse-Swift Playground Which contains examples from the Parse-swift Devs themselves but nothing.

CodePudding user response:

The statement you created in your query is essentially an AND statement and requires the key to be in all 3 columns. The API documentation shows that there's an or query constraint available. I recommend looking through the API documentation to see all of the features that are available, as the Playgrounds isn't intended to implement every feature of Parse-Swift.

You can accomplish your goal by doing:

let query1 = Wallpaper.query(Wallpaper.CodingKeys.firstArtist.rawValue == testString)

let query2 = Wallpaper.query(Wallpaper.CodingKeys.secondArtist.rawValue == testString)

let query3 = Wallpaper.query(Wallpaper.CodingKeys.thirdArtist.rawValue == testString)            

let subQueries = [query1, query2, query3]
let query = Wallpaper.query(or(queries: subQueries))

// Using async/await, but completion block will be similar to the code you provided
do {
  let wallpapers = try await query.findAll()
  print(wallpapers)
} catch {
  //Handle error
}
  • Related