Alek Piasecki Alek Piasecki - 1 month ago 7x
iOS Question

How to modify query with .whereKey, but utilizing multiple different parameters in Xcode?

I am creating an application in Xcode, using swift, that is pulling information from a parse-server database (hosted by Heroku). There is a convinient way to modify the query, which can be set up, for example, as follows:

let getFollowedUserQuery = PFQuery(className: "Followers")
getFollowedUserQuery.whereKey("Follower", equalTo: (PFUser.currentUser()?.objectId)!)

However, I would like the query to query based on multiple parameters. Here it is currently only checking one column in the DB. Is there any way to modify this query using .whereKey (or something of the same sort) such that it will check multiple parameters/columns in the DB. Essentially, it will be checking these columns based on search parameters input by the user. But the user can select multiple parameters... so the query needs to return only objects which fit all parameters, not just one. Is there a way to do that?


Did you check the docs? It says:

You can give multiple constraints, and objects will only be in the results if they match all of the constraints. In other words, it’s like an AND of constraints.

In your case it would be:

let getFollowedUserQuery = PFQuery(className: "Followers")
getFollowedUserQuery.whereKey("Follower", equalTo: PFUser.currentUser()?.objectId ?? "")
getFollowedUserQuery.whereKey("AnotherKey1", equalTo: anotherObject1)
getFollowedUserQuery.whereKey("AnotherKey2", notEqualTo: anotherObject2)

Another option is to use NSPredicate:

let predicate = NSPredicate(format: "Follower = '\(PFUser.currentUser()?.objectId ?? "")' AND AnotherKey1 = '\(anotherObject1)' AND AnotherKey2 != '\(anotherObject2)'")
let query = PFQuery(className: "Followers", predicate: predicate)