0

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")
}
Tom Fox
  • 897
  • 3
  • 14
  • 34
Khledon
  • 193
  • 5
  • 23

2 Answers2

0

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.

Khledon
  • 193
  • 5
  • 23
  • 2
    _User is a special class, as it's `built-in` - If you were trying to access a class you've made, you wouldn't use an underscore. You'll notice that there are other special classes like `_Role` and `_Session`. For this reason, there is a class in the SDK to match, in this case, `PFUser`. You'll see here: https://docs.parseplatform.org/ios/guide/#querying how you should be creating your query. – William George Mar 15 '19 at 23:43
  • 1
    Although there is an explanation of how to query the user class using the `PFUser` class this could be mentioned in the Queries section of the iOS docs too. I would welcome a pull request to address this on the [docs repo](https://github.com/parse-community/docs) – Tom Fox Mar 23 '19 at 21:54
0

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.

Tom Fox
  • 897
  • 3
  • 14
  • 34