本文介绍了在Alamofire设置超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Alamofire 4.0.1和我想为我的请求设置超时。我尝试了问题中给出的解决方案:
I am using Alamofire 4.0.1 and I want to set a timeout for my request. I tried the solutions gived in this question:
在第一种情况中,它会抛出 NSURLErrorDomain (超时设置正确):
In the first case, it throws a NSURLErrorDomain (timeout is set correctly):
let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForRequest = 10 let sessionManager = Alamofire.SessionManager(configuration: configuration) sessionManager.request("yourUrl", method: .post, parameters: ["parameterKey": "value"]) .responseJSON { response in switch (response.result) { case .success: //do json stuff break case .failure(let error): if error._code == NSURLErrorTimedOut { //timeout here } print("\n\nAuth request failed with error:\n \(error)") break } }在第二种情况中,超时未被替换,仍然设置为60秒。
In the second case, the time out is not replaced and still set as 60 seconds.
let manager = Alamofire.SessionManager.default manager.session.configuration.timeoutIntervalForRequest = 10 manager.request("yourUrl", method: .post, parameters: ["parameterKey": "value"])我在ios 10.1中运行
我的代码:(它不起作用)
let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForRequest = 10 // seconds configuration.timeoutIntervalForResource = 10 let alamoFireManager = Alamofire.SessionManager(configuration: configuration) alamoFireManager.request("my_url", method: .post, parameters: parameters).responseJSON { response in switch (response.result) { case .success: //Success.... break case .failure(let error): // failure... break } }Solved Alamofire github thread: Alamofire 4.3.0设置超时抛出NSURLErrorDomain错误#1931
推荐答案请试试这个: -
let request = NSMutableURLRequest(url: URL(string: "")!) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.timeoutInterval = 10 // 10 secs let values = ["key": "value"] request.httpBody = try! JSONSerialization.data(withJSONObject: values, options: []) Alamofire.request(request as! URLRequestConvertible).responseJSON { response in // do whatever you want here }更多推荐
在Alamofire设置超时
发布评论