简体   繁体   中英

Alamofire called twice

I'm trying to use Alamofire but it`s really weird. 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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