Search code examples
iosswiftparse-platformswift2

find Parse objects that match Pointer value?


I'm trying to write a Swift query that will get the object in the Avatar table that matches the User's avatar Pointer column. The following query doesn't pull any results:

var userAvatar = self.user["avatar"]
let avatarQuery = PFQuery(className: "Avatar")
avatarQuery.whereKey("objectId", equalTo: userAvatar)
avatarQuery.limit = 1
avatarQuery.findObjectsInBackgroundWithBlock{
    (results: [PFObject]?, error: NSError?) -> Void in

    if error != nil {
        print(error)
    } else if let results = results as? [PFObject]! {
        for result in results {

I think the problem is that the whereKey clauses is looking for a String, yet userAvatar is a PFObject. I tried converting the PFObject to String but that's not possible.

Am I overthinking this? How can I just get the Avatar object that matches the PFObject stored in User -> avatar(Pointer)?

Thanks!

EDIT: Thanks to Daniel, this is the working code (I think adding the includeKey might have helped too):

let userAvatar = self.user["avatar"] as! PFObject
let avatarQuery = PFQuery(className: "Avatar")
avatarQuery.whereKey("objectId", equalTo: userAvatar.objectId!)
avatarQuery.includeKey("avatar")
avatarQuery.limit = 1
avatarQuery.findObjectsInBackgroundWithBlock{
    (results: [PFObject]?, error: NSError?) -> Void in

        if error != nil {
            print(error)
        } else if let results = results as? [PFObject]! {

            for result in results {

Solution

  • So I think your problem is that you should not be comparing a string to a PfObject the object is not a string but a price of the object may be a string so you should compare something like the object.id to a string. If that makes sense.