简体   繁体   English

Alamofire打了两次电话

[英]Alamofire called twice

I'm trying to use Alamofire but it`s really weird. 我正在尝试使用Alamofire,但它真的很奇怪。 I'm calling this function and debugging, it's being called twice, I don't know why. 我正在调用这个函数和调试,它被调用两次,我不知道为什么。

In the first time it just jump all the function without doing anything and the second time runs normaly. 在第一次它只是跳过所有功能而没有做任何事情,第二次正常运行。

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

This ViewController it's being called by a PerformSegue WithIdentifier. 这个ViewController由PerformSegue WithIdentifier调用。 I don't know what could be. 我不知道会是什么。

It could be the debug statements itself causing the extra request. 它可能是调试语句本身导致额外的请求。 For example if you do something like this to see what the request will look like then it will actually make the request 例如,如果您执行此类操作以查看请求的外观,那么它实际上会发出请求

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....

I am assuming you are putting a break point on the request line. 我假设你在请求行上设置了一个断点。 When you do this its gonna trigger twice. 当你这样做时,它会触发两次。 First time when request is sended and second time when you get the response. 第一次发出请求时,第二次收到回复。 Compiler sees the completion block as 1 line and triggers on the same line again. 编译器将完成块视为1行并再次在同一行上触发。

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

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

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