Facebook iOS SDK& Swift - 如何创建依赖批处理请求?

时间:2015-05-05 04:09:00

标签: ios swift facebook-graph-api

当第二个(子)请求依赖于第一个(父)请求时,我试图弄清楚如何编写FB Graph API请求(FB SDK 4.0和Swift)。具体来说,我想获得一个用户的专辑和每张专辑的封面照片。

  1. me / albums?fields = name,cover_photo< - 获取用户相册请求

  2. / 888474748< - 获取封面照片请求

  3. 关于此问题的文档非常含糊,FBSDKGraphRequestConnection的API文档提到了方法 addRequest:completionHandler:batchParameters:可以接受诸如" name"之类的参数。和"依赖"。这似乎是我正在寻找的方法,但我可以在Obj-C或Swift中找到它的使用示例。

    看起来应该是这样的吗?谢谢!

     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?
    

1 个答案:

答案 0 :(得分:2)

弄清楚了。

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