Frederick C. Lee
Frederick C. Lee

Reputation: 9493

How do I extract string/image attributes from PFUser (pointer) within a PFObject?

I'm trying to disseminate a PFObject that contains a pointer to a PFUser ('owner'):

enter image description here

Here's my fetch code:

public func collectHashTags(#sender:UIViewController, withinGeoCoordinate geoPoint:PFGeoPoint) {
    var query = PFQuery(className:"Tag")
    query.includeKey("owner")  //...note: this doesn't give me the owner object.

    //    query.whereKey("lastGeo", nearGeoPoint:geoPoint, withinKilometers:2)

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

        let tagArray = query.findObjects() as [PFObject]
        if tagArray.count > 0 {
            for x in 0...(tagArray.count - 1) {
                disseminateTags(tag:tagArray[x])   // ...load global Tag Array.
            }
        } else {
            showAlert(sender, withTitle:"No Data", withMessage:"The HashTag file is empty.")
        }

    });

}

Below is 'tag', a PFObject that has the data I want to get:

private func disseminateTags(#tag:PFObject) {

    let owner:PFUser = tag[gParseOwner] as PFUser

    let parseID = tag.objectId as String
    let displayName = tag[gParseDisplayName] as String
    let description = tag[gParseDescription] as String
    let radius = tag[gParseRadius] as CGFloat
    let tagPublicFlag = tag[gParsePublic] as Bool
    let startTime = tag[gParseStartTime] as NSDate
    let endTime = tag[gParseEndTime] as NSDate
    let media = tag[gParseMedia]? as [Int]

    let startTimeString:String = startTime.toString()
    let endTimeString:String = endTime.toString()

    // Create the local hashTag object:
    let myTag = hashTag(parseID: parseID, name: displayName, havingDescription: description, owner: owner)
    myTag.radius = radius
    myTag.publicFlag = tagPublicFlag
    myTag.startTimeStamp = startTimeString
    myTag.endTimeStamp = endTimeString

    gTagArray.append(myTag)

}

As you can see, 'tag' has data. I want to disseminate 'owner', a PFUser object:

(lldb) po tag
<Tag: 0x7fac1f91c950, objectId: 5J1dNXUdaM, localId: (null)> {
    Media =     (
        3,
        2,
        1
    );
    accessPemission = 1;
    description = "Hashbrown Potatoes";
    displayName = Smirf123;
    endTime = "2104-12-17 20:20:00 +0000";
    geoPoint = "<PFGeoPoint: 0x7fac1f99a990, latitude: 40.000000, longitude: -30.000000>";
    owner = "<PFUser: 0x7fac1f993370, objectId: hk7zYRK4xB>";
    public = 1;
    radius = 2000;
    startTime = "2104-12-15 20:20:00 +0000";
}

But I'm running into a problem. Apparently I got the 'owner'(notice its objectID, see above), but can't see it in the debugger: enter image description here


Second Try:

Using 'includeKey("owner") didn't help me. So I commented it out so the 'owner' attribute is included with its siblings in the 'Tag' class.

Then I tried to do a query on the owner (id) field (ref: https://www.parse.com/docs/ios_guide#queries/iOS):

public func collectHashTags(#sender:UIViewController, withinGeoCoordinate geoPoint:PFGeoPoint) {
    var query = PFQuery(className:"Tag")
   // query.includeKey("owner")

    //    query.whereKey("lastGeo", nearGeoPoint:geoPoint, withinKilometers:2)

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

        let tagArray = query.findObjects() as [PFObject]


        if tagArray.count > 0 {
            for x in 0...(tagArray.count - 1) {
                disseminateTags(tag:tagArray[x])   // ...load global Tag Array.
            }
        } else {
            showAlert(sender, withTitle:"No Data", withMessage:"The HashTag file is empty.")
        }

    });

}

private func disseminateTags(#tag:PFObject) {

    let owner = tag[gParseOwner] as PFUser

    owner.fetchIfNeededInBackgroundWithBlock {
        (object: PFObject!, error: NSError!) -> Void in

        if error != nil {
            println("Error: \(error)")
        } else {
            println("Fetched Owner: \(object)")
        }
    }
}

This time the NSError flagged with the following:

...Error: object not found for get (Code: 101, Version: 1.5.0)
...Error: object not found for get (Code: 101, Version: 1.5.0)
...Error: object not found for get (Code: 101, Version: 1.5.0)
...Error: object not found for get (Code: 101, Version: 1.5.0)
Error: Error Domain=Parse Code=101 "The operation couldn’t be completed. (Parse error 101.)" UserInfo=0x7ff9d260af40 {error=object not found for get, code=101}
Error: Error Domain=Parse Code=101 "The operation couldn’t be completed. (Parse error 101.)" UserInfo=0x7ff9d410ee80 {error=object not found for get, code=101}
Error: Error Domain=Parse Code=101 "The operation couldn’t be completed. (Parse error 101.)" UserInfo=0x7ff9d410ee20 {error=object not found for get, code=101}
Error: Error Domain=Parse Code=101 "The operation couldn’t be completed. (Parse error 101.)" UserInfo=0x7ff9d41082a0 {error=object not found for get, code=101}


Third Try (using follow-on fetchIfNeeded()):

public func collectHashTags(#sender:UIViewController, withinGeoCoordinate geoPoint:PFGeoPoint) {
    var query = PFQuery(className:"Tag")

    //    query.whereKey("lastGeo", nearGeoPoint:geoPoint, withinKilometers:2)

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

        let tagArray = query.findObjects() as [PFObject]?

        if tagArray != nil {
            let myTag = tagArray![0]
            println("***myTag: \(myTag)")
            let myName = myTag["displayName"] as String
            println("displayName: \(myName)")

            let myOwner = myTag["owner"] as PFUser
            myOwner.fetchIfNeeded()

            println("**** myOwner: \(myOwner)")
        }


    });

}

The output:

***myTag: <Tag: 0x7fa5d87e8f70, objectId: 5J1dNXUdaM, localId: (null)> {
    Media =     (
        3,
        2,
        1
    );
    accessPemission = 1;
    description = "Hashbrown Potatoes";
    displayName = Smirf123;
    endTime = "2104-12-17 20:20:00 +0000";
    geoPoint = "<PFGeoPoint: 0x7fa5de104770, latitude: 40.000000, longitude: -30.000000>";
    owner = "<PFUser: 0x7fa5de108070, objectId: hk7zYRK4xB>";
    public = 1;
    radius = 2000;
    startTime = "2104-12-15 20:20:00 +0000";
}
displayName: Smirf123
2014-12-14 15:09:12.004 Bliss2[4757:181319] Error: object not found for get (Code: 101, Version: 1.5.0)
**** myOwner: <PFUser: 0x7fa5de108070, objectId: hk7zYRK4xB, localId: (null)> {
}

I'm lost here.
Disseminating a PFUser pointer with the hosting record should NOT be this difficult.
How can I get/extract the (string & image) attributes from 'owner' (PFUser)?

Upvotes: 0

Views: 466

Answers (1)

user3321446
user3321446

Reputation:

So the key to understanding this issue is realizing that the object within the <..> when you print out the PFObject is essentially an indexable array.

So if, say "tag" is the PFObject you are getting back and you wanted to grab the Media attributes of it (as indicated in the println output), then you would use: tag["Media"].

In my code I grabbed the PFObject from parse by objectID but I don't think that really matters.

Hope this answers your question.

Upvotes: 0

Related Questions