简体   繁体   中英

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?

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.

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)
    }
})

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 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM