简体   繁体   English

迅速的alamofire请求json异步

[英]swift alamofire request json asynchronous

I tried to send a request to get a JSON from Amazon by Alamofire, but it asynchronized. 我试图通过Alamofire发送一个从Amazon获取JSON的请求,但是它是异步的。 it comes back to the caller function before getting the response from Amazon. 在从亚马逊获得响应之前,它会返回到调用者函数。

public func getJSON(fileName: String) -> JSON?{
    let url = "http://s3.eu-west-3.amazonaws.com" + fileName
    print(self.json)

    if self.json == nil {
        Alamofire.request(url)
            .responseJSON { response in
                if let result = response.result.value {
                    self.json = JSON(result)
                }

        }
       return self.json
    }
    else{
        return nil
    }
}

public func initTableView(){
    let myJson = AmazonFiles.shared.getJSON(fileName: "/jsonsBucket/myJson.json")
    print(myJson["id"])
}

the object myJson in initTableView function is always nil. initTableView函数中的对象myJson始终为nil。

How could I resolve this issue? 我该如何解决这个问题?

Instead of returning JSON? 而不是返回JSON? in the method signature, use a completion closure like this: 在方法签名中,使用如下所示的完成闭包:

public func getJSON(fileName: String, completion: ((JSON?) -> Void)?) {
    let url = "http://s3.eu-west-3.amazonaws.com" + fileName
    Alamofire.request(url).responseJSON { response in
        if let result = response.result.value {
            completion?(JSON(result))
        } else {
            completion?(nil)
        }
    }
}

And call the method like this: 并调用如下方法:

getJSON(fileName: "/jsonsBucket/myJson.json") { json in
    print(json)
}

Or: 要么:

getJSON(fileName: "/jsonsBucket/myJson.json", completion: { json in
    print(json)
})

You need to implement a completion handler, have a look on this article . 您需要实现一个完成处理程序,看看本文

The completion handler is the code that we get to provide to get called when it comes back with those items. 完成处理程序是我们提供的代码,当它们与这些项目一起返回时将被调用。 It's where we can work with the results of the call: error checking, saving the data locally, updating the UI, whatever. 在这里我们可以处理调用的结果:错误检查,本地保存数据,更新UI等。

typealias completionHandler = (JSON?) -> Void // this is your completion handler

public func getJSON(fileName: String, completionHandler: @escaping completionHandler) -> JSON?{
    let url = "http://s3.eu-west-3.amazonaws.com" + fileName
    if self.json == nil {
        Alamofire.request(url)
            .responseJSON { response in
                if let result = response.result.value {
                  completionHandler(json) // this will fire up your completion handler,
                }
        }
    }
    else{
        completionHandler(nil)
    }
}

And you can use it like this. 您可以像这样使用它。

getJSON(fileName: "fileName") { (json) in
    // this will fire up when completionhandler clousre in the function get triggered
    //then you can use the result you passed whether its JSON or nil
    guard let result = json  else { return } // unwrap your result and use it
    print(result)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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