Marcelo Pontes Machado
Marcelo Pontes Machado

Reputation: 183

Make a Query with a Pointer in Parse and Swift

I'm trying to make a Query with a Pointer in Parse.

Basically I have two classes "commentsTable" and "_User", I want to get the comment of the user from class "commentsTable" on a determined post, and then get the username and the profile_pic from the class "_User"

_User Class enter image description here

commentsTable Class enter image description here

    func loadAndShowComments(){
    let query2 = PFQuery(className: "commentsTable")
    query2.orderByDescending("createdAt")
    query2.whereKey("newsColumns", equalTo: printteste!)
    query2.includeKey("username")

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

        if let objects = objects as [PFObject]? {

            for object in objects {

                print(object["commentColumn"])

            }

        }


        for cardset in objects! {
            var lesson = cardset["username"] as! PFObject
            var name = lesson["username"] as! String
            print("By user: \(name)")
        }

I'm able to see the query, I print the result an I have the following output:

This is a post!
This is a test post!
By user: [email protected]
By user: mmachado

And in my app I display this informations inside a TableView, I'm successfully can show the results for the Query in the func cellForRowAtIndexPath:

    if let usuarioComentario = object?["commentColumn"] as? String {
        cell?.usuarioComentario?.text = usuarioComentario
    }

But I'm no able to return the values of my other class, _User

I think I misunderstood some concept but at this point I don't know what concept, any ideas?

Thanks.

Upvotes: 2

Views: 1636

Answers (1)

Russell
Russell

Reputation: 3089

By using query2.includeKey("username") you are already retrieving all of the User data associated with each commentsTable object.

You can access the related User data using the following.

if let commentUser = object["username"] as? PFUser {
    let name = commentUser["username"] as! String
    let profilePicture = commentUser["profile_pic"] as! PFFile
}

You need to store the query results to an array for later use if you aren't already. If you are using Parse's provided PFQueryTableViewController this will be handled for you by implementing the queryForTable() method and the results are automatically stored in an array of dictionaries called objects.

It is also worth noting that you will have to still have to load the PFFile because they are not included in query results. You will want to assign the PFFile to a PFImageView and then call loadInBackground. See the example below.

let imageView = PFImageView()

// Set placeholder image
imageView.image = UIImage(named: "placeholder")

// Set remote image
imageView.file = profilePicture

// Once the download completes, the remote image will be displayed
imageView.loadInBackground { (image: UIImage?, error: NSError?) -> Void in
    if (error != nil) {
        // Log details of the failure
        println("Error: \(error!) \(error!.userInfo!)")            
    } else {
        println("image loaded")
    }
}

Lastly, I'd recommend changing the name of the User pointer within commentsTable from "username" to "user" so there is no confusion with the username field of the user class. Here's a link to a great tutorial which you may also find helpful

Upvotes: 3

Related Questions