PFQuery returning blank query

88 Views Asked by At

I'm trying to run a PFQuery that will populate an array of custom structs.

Everything looks ok, but when I run the code the query returned is empty. I also tried this using PFUser.Query, which worked, but did not return a value for objectId, so tried to query the class instead.

Here is what I have:

var parseUsers:[ParseUsers] = []
var allUsers:[PFObject] = []
let query = PFQuery(className:"User")
let currentUser = PFUser.current()
query.whereKey("username", notEqualTo: currentUser?.username)
do {
    allUsers = try (query.findObjects())
    for i in allUsers{
        var pImage = UIImage()
        if i["profileImage"] != nil{
            let imageFile = i["profileImage"] as? PFFileObject
            imageFile?.getDataInBackground { (imageData: Data?, error: Error?) in
                if let error = error {
                    print(error.localizedDescription)
                } else if let imageData = imageData {
                    pImage = UIImage(data:imageData)!
                }
        }

        }

       let currentUserInfo = ParseUsers(objectID:i["objectId"] as! String,
                           username:i["objectId"] as! String,
                           userWorkouts:i["userWorkouts"] as! [Dictionary<String, String>],
                           profileImage:pImage)
        parseUsers.append(currentUserInfo)


    }
} catch {
    print("Error")
}
2

There are 2 best solutions below

2
On BEST ANSWER

Found out what the problem was!

For anyone experiencing similar issues in the future, when determining the class you want to query, you need to out _ in front. In the case above it would be let query = PFQuery(className:"_User").

Weird, as I couldn't see this mentioned in the parse documentation anywhere.

0
On

As mentioned by @William George in the Querying subsection of Users you can see how to construct a query on on the _User class.

A Below is an example of this (lifted straight from the docs):

var query = PFUser.query()
query.whereKey("gender", equalTo:"female")
var girls = query.findObjects()

You can query your Parse Server with PFUser just like with PFQuery but it can only be used on the _User class.