user1555112
user1555112

Reputation: 1987

ios swift parse: how to collect pointer data

I have this query:

        var query = PFQuery(className: "CardSet")
        query.whereKey("user", equalTo: PFUser.currentUser())
        query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

            if (error == nil) {

                // found

            }else{
                println(error.userInfo)
            }
        }

The result looks like this:

<CardSet: 0x7c892120, objectId: 2yty7cpnyF, localId: (null)> {
    ACL = "<PFACL: 0x7be65450>";
    lesson = "<Lesson: 0x7beaabd0, objectId: JV7trFTx5Z>";
    name = "I am your CardSet";
    public = 1;
    user = "<PFUser: 0x7be834a0, objectId: baz8ObNsmM>";
}

Class "CardSet" has a pointer "lesson" to class "Lesson". What can I do to have Lesson.name available in the query result? Or do I need to query a second time? That would be bad because of the amount of requests to parse...? Is there any better way getting all pointed data in just one query?

Upvotes: 1

Views: 2573

Answers (3)

G&#246;khan Geyik
G&#246;khan Geyik

Reputation: 313

first you must use includekey then save data as PFObject in findobjectwithdatablock -> if data is PFUser save as PFUser else save as PFObject

Example :

var veriable  = (PFUser)() 
YourQueryFindObjectBlock {  
self.veriable = result["pointerCol"] as PFUser // if pointer has user data 
}

Upvotes: 0

James Frost
James Frost

Reputation: 6990

You can tell Parse to fetch a related object using the includeKey method:

var query = PFQuery(className: "CardSet")
query.whereKey("user", equalTo: PFUser.currentUser())
query.includeKey("lesson")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) in
    for cardset in objects {
        var lesson = cardset["lesson"] as PFObject
        var name = lesson["name"] as String
        println("retrieved related lesson: \(post) with name: \(name)")
    }
}

  Mentioned in the Relational Queries section of the documentation.

Upvotes: 10

Daij-Djan
Daij-Djan

Reputation: 50089

you get passed an array of objects so you have to get the CardSet from the array:

query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
    if (error == nil) {
        for object in objects { //note: if objects is always of size 1, just use objects[0]
            var cardset = object as CardSet //get the entry
            var lesson = cardset.lesson
        }
    }else{
        println(error.userInfo)
    }
})

Upvotes: 0

Related Questions