Handle timeout with Alamofire

后端 未结 9 1653
暗喜
暗喜 2020-12-01 05:24

Is it possible to add timeout handler for Alamofire request?

In my project I use Alamofire this way:

init() {
    let configuration = NSURLSessionCon         


        
9条回答
  •  -上瘾入骨i
    2020-12-01 05:49

    You can compare error._code and if it is equal to -1001 which is NSURLErrorTimedOut then you know this was a timeout.

    let manager = Alamofire.SessionManager.default
    manager.session.configuration.timeoutIntervalForRequest = 120
    
    manager.request("yourUrl", method: .post, parameters: ["parameterKey": "value"])
            .responseJSON {
                response in
                switch (response.result) {
                case .success: // succes path 
                case .failure(let error):
                    if error._code == NSURLErrorTimedOut {
                        print("Request timeout!")
                    }
                }
            }
    

提交回复
热议问题