Two Query Constraints On One Key with Parse and Sw

2019-02-23 19:07发布

问题:

guys, I am trying to get all the PFUsers whose username is not included in an array (dontShowUsers. I also want to limit the query with the variable name, which gets all users whose username is equal a name the user searches. However when I do this:

findUsers.whereKey("username",containsString:name)
findUsers.whereKey("username",notContainedIn:dontShowUsers)

Every user shows. It peforms both these queries, and adds them together. I want to constrain the first whereKey with the second whereKey. What would be the best way to go about this?

回答1:

this is one way

println(geoPoint)
user["location"] = geoPoint
let rejectedUsers: [String] = user["rejected"].copy() as [String]
let acceptedUsers: [String] = user["accepted"].copy() as [String]
let ignoredUsers = rejectedUsers + acceptedUsers

var query = PFUser.query()
query.whereKey("location", nearGeoPoint: geoPoint)
query.whereKey("username", notEqualTo: user.username)
query.whereKey("gender", equalTo: user["interestedIn"])
query.whereKey("username", notContainedIn: ignoredUsers)
query.limit = 10
query.findObjectsInBackgroundWithBlock({
    (objects: [AnyObject]!, error2: NSError!) -> Void in
    if error2 != nil {
        println(error2)
    } else {
        if objects.isEmpty {
            println("empty query")
        } else {
            for object in objects {
                self.usernames.append(object.username)
                self.userImages.append(object["image"] as NSData)

notice how i had to copy my user["rejected"] array from parse then i used + to create "ignoredUsers"

then i was able to use:

query.whereKey("username", notContainedIn: ignoredUsers)



回答2:

I would say that you can solve this without using 2 constraints, just before you execute the query, check if name is in the the array, and if it's not, just execute the query.