iosparse-platformswift2parse-serverpfquery

Swift Wrongly Reading PFQuery Return


I have PFQuery function in Swift that's supposed to act based on the returned objects. I've tried if error == nil, if objects != nil and if error == nil && objects != nil, but it always acts as if the keys "otherUser" and "responded" were equalTo what the query asked for, although "otherUser" is not found in the class "Requests" and "responded" equals to "False". (I shortened the actual full-length query because it was too long).

let query = PFQuery(className: "Requests")
        query.whereKey("otherUser", equalTo: PFUser.currentUser()!.objectForKey("username") as! String)
        query.whereKey("responded", equalTo: "True")
        query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
            if error == nil && objects != nil{
                print(objects)
                print("disabling confirm button")
                self.stopTime = self.defaults.integerForKey("intKey")
            }else{
                print("Did not respond yet.")
                self.counter = 1200
                self.storyboard
            }
        })
}

Solution

  • Even if otherUser is not found or responded is False, query still will NOT return error, and objects will just be an empty array. And an empty array is not nil, it exists in the heap.

    So as long as you are doing everything right, even if the query does not match your constraint, it still returns an empty array instead of an error.

    If you want to check if the query has returned any results, you can use if (objects.count) to check if the returned results is empty or not.