繁体   English   中英

Alamofire打了两次电话

[英]Alamofire called twice

我正在尝试使用Alamofire,但它真的很奇怪。 我正在调用这个函数和调试,它被调用两次,我不知道为什么。

在第一次它只是跳过所有功能而没有做任何事情,第二次正常运行。

override func viewDidLoad() {
    super.viewDidLoad()

    download{
        //do stuffs
    }

}

func download(completed: @escaping DownloadComplete){

    Alamofire.request("https://httpbin.org/get").responseJSON { response in
        print(response.request ?? "")  // original URL request
        print(response.response ?? "") // HTTP URL response
        print(response.data ?? "")     // server data
        print(response.result)   // result of response serialization

        if let JSON = response.result.value {
            print("JSON: \(JSON)")
        }
        completed()
    }
}

这个ViewController由PerformSegue WithIdentifier调用。 我不知道会是什么。

它可能是调试语句本身导致额外的请求。 例如,如果您执行此类操作以查看请求的外观,那么它实际上会发出请求

let test = session.request(url);

print("Debug print request")
// this will make first call even though response is not handled
debugPrint(test)

// second call
session.request(url).responseJson....

我假设你在请求行上设置了一个断点。 当你这样做时,它会触发两次。 第一次发出请求时,第二次收到回复。 编译器将完成块视为1行并再次在同一行上触发。

如果没有重复的旧连接,请检查故事板(连接检查器,⌘+⌥+ 6)。

暂无
暂无

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

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