2

I'm trying to figure out how to compose FB Graph API requests (FB SDK 4.0 and Swift) when the second (child) request is dependent on the first (parent) request. Specifically I would like to get a user's albums and each album's cover photo.

  1. me/albums?fields=name,cover_photo <-- Get user albums request

  2. /888474748 <-- Get cover photo request

The documentation is very vague regarding this and API docs for FBSDKGraphRequestConnection mention that method addRequest:completionHandler:batchParameters: can accept parameters such as "name" and "depends_on". This appears to be the method I'm looking for but I can find an example of its use in Obj-C or Swift.

Should it look something like this? Thanks!

 let albumRequest = FBSDKGraphRequest(graphPath: "me/albums?fields=name,cover_photo", parameters: nil)
    let albumCoverRequest = FBSDKGraphRequest(graphPath: "cover_photo_id", parameters: nil) //what should this look like? jsonpath?
    let graphConnection = FBSDKGraphRequestConnection()
    graphConnection.addRequest(albumRequest, completionHandler: { (connection:FBSDKGraphRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
        if(error != nil){
            println(error)
        }else{

        }
        },batchParameters: ["name" : "albums"])

    graphConnection.addRequest(albumRequest, completionHandler: { (connection:FBSDKGraphRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
        if(error != nil){
            println(error)
        }else{

        }
        },batchParameters: ["depends_on" : "albums"]) //should this be some jsonpath expression?
Nick
  • 19,198
  • 51
  • 185
  • 312

1 Answers1

3

Got it figured out.

let albumRequest = FBSDKGraphRequest(graphPath: "me/albums?fields=name,cover_photo", parameters: nil)
    let albumCoverRequest = FBSDKGraphRequest(graphPath: "?ids={result=albums:$.data.*.cover_photo}", parameters: nil) // use jsonpath syntax to "inject" parent results into "child" request
    let graphConnection = FBSDKGraphRequestConnection()
    graphConnection.addRequest(albumRequest, completionHandler: { (connection:FBSDKGraphRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
        if(error != nil){
            println(error)
        }else{
            println(result)
        }
        },batchParameters: ["name" : "albums"]) //Set "parent" batch alias

    graphConnection.addRequest(albumCoverRequest, completionHandler: { (connection:FBSDKGraphRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
        if(error != nil){
            println(error)
        }else{
            println(result)
        }
        },batchParameters: ["depends_on" : "albums"]) //depend on parent batch alias
    graphConnection.start()
Nick
  • 19,198
  • 51
  • 185
  • 312
  • 1
    How do you actually access each albums results however? I haven't had any luck turning result into an array... – Matt May 11 '15 at 17:06