0
votes

I have a class named Post which stored the data of Image, Text and the upLoader. The uploader is link or a pointer to the User class.

When I was testing When the current user is equal to the user which is pointed, everything is good. However, when the current user is not equal to the uploader, I cannot got the data from the uploader such as username and email. The only data I could retrieve is [ { }]

Code of Query

    let query = PFQuery(className:"Post")

    query.orderByDescending("createdAt")

    query.findObjectsInBackgroundWithBlock {

        (objects: [PFObject]?, error: NSError?) -> Void in



        if error == nil {

            print("Successfully retrieved \(objects!.count) scores.")

            if let objects = objects! as [PFObject]! {

                for object in objects {



                    self.message.append(object["message"] as! String)

                    self.imageFiles.append(object["imageFile"] as! PFFile)

                    self.user.append(object["upLoader"] as! PFUser )

                    self.createdAT.append(object.createdAt!)



                    self.tableView.reloadData()



                }





            }

        } else {

            print("Error: \(error!) \(error!.userInfo)")

        }

Can anyone help me? Thanks

1

1 Answers

0
votes

By default, Parse queries don't include the actual objects that are referenced by pointers, so your upLoader is just a reference to the object ID, not the actual object. To include the actual object, you just need to add includeKey to your query. For example:

let query = PFQuery(className:"Post")
query.includeKey("upLoader")  // This should do the trick.
query.orderByDescending("createdAt")

query.findObjectsInBackgroundWithBlock {
    (objects: [PFObject]?, error: NSError?) -> Void in

    if error == nil {
        print("Successfully retrieved \(objects!.count) scores.")

        if let returnedObjects = objects {
            for object in returnedObjects {
                self.message.append(object["message"] as! String)
                self.imageFiles.append(object["imageFile"] as! PFFile)
                self.user.append(object["upLoader"] as! PFUser )
                self.createdAT.append(object.createdAt!)

                dispach_async(dispatch_get_main_queue(), { () -> Void in
                    //UI stuff need to be on main thread
                    self.tableView.reloadData()
                })
            }
        }
    } else {
        print("Error: \(error!) \(error!.userInfo)")
    }
}