AlamoFire不尊重超时间隔

class APIClient { var user = User() let alamoFireManager : Alamofire.Manager? let center = NSNotificationCenter.defaultCenter() init(){ let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.timeoutIntervalForRequest = 4 // seconds configuration.timeoutIntervalForResource = 4 self.alamoFireManager = Alamofire.Manager(configuration: configuration) } func test(){ //This does not respect the 4 second time out. Why? self.alamoFireManager!.request(.POST, CONSTANTS.APIEndpoint+"/test", parameters: parameters).responseJSON { (req, res, json, error) in if let json = self.handleAPIResponse(req, res: res, json_data: json, error: error){ } } } 

你确定4秒超时没有执行吗? 我创造了一个实验:

  let center = NSNotificationCenter.defaultCenter() var alamoFireManager : Alamofire.Manager? let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.timeoutIntervalForRequest = 4 // seconds configuration.timeoutIntervalForResource = 4 self.alamoFireManager = Alamofire.Manager(configuration: configuration) self.alamoFireManager!.request(.POST, "http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/2", parameters: nil).responseJSON { (req, res, json, error) in println("First json \(json)") println("First error \(error)") } self.alamoFireManager!.request(.POST, "http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/6", parameters: nil).responseJSON { (req, res, json, error) in println("Second \(json)") println("Second \(error)") } 

和我输出的控制台输出:

 First json Optional({ name = timeoutTest; value = 2; }) First error nil Second nil Second Optional(Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7f91dc8223e0 {NSErrorFailingURLKey=http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/6, NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/6}) 

我的示例中的url目前处于closures状态,我将尝试将其重新在线。 您可以使用示例中的URL来testing。 通过在最后设置一个不同的数字,您可以修改超时(以秒为单位)。

我已经使用了Alamofire的最后一个版本的cocoapods。

您需要为请求pipe理器创build一个全局variables:

 var alamoFireManager = Alamofire.Manager.sharedInstance 

并在configuration自定义参数后:

  let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.timeoutIntervalForRequest = 4 // seconds configuration.timeoutIntervalForResource = 4 self.alamoFireManager = Alamofire.Manager(configuration: configuration) 

这里是Swift 3.0 / Alamofire 4.0代码来得到一个5秒超时的alamofireManager:

  let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForResource = 5 // seconds let alamofireManager = Alamofire.SessionManager(configuration: configuration) 

我想这个是适合你的,注意你必须在函数外部声明Alamofire.Manager的实例,请看下面的代码

 //Declare it out side the function var alamoFireManager : Alamofire.Manager! func callUrl() { let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.timeoutIntervalForResource = 10800 // seconds configuration.timeoutIntervalForRequest = 10800 // seconds alamoFireManager = Alamofire.Manager(configuration: configuration) alamoFireManager.request(.POST, url, parameters: dir as? [String : AnyObject], encoding: .JSON, headers: [ "Content-Type": "application/json"]) .responseJSON { response in } } 

希望这可以帮助你

  init(){ let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.timeoutIntervalForResource = 4 configuration.timeoutIntervalForRequest = 4 self.alamoFireManager = Alamofire.Manager(configuration: configuration) self.alamoFireManager!.request(.POST, CONSTANTS.APIEndpoint+"/test", parameters: parameters).responseJSON { (req, res, json, error) in if let json = self.handleAPIResponse(req, res: res, json_data: json, error: error){ } } } 

在同一个初始化函数中使用self.alamoFireManager!.request ,超时延迟只针对请求而不是整个函数。 可能会有帮助。

当我使用上面的方法时,我得到了NSURLErrorDomain。 这是我的代码(swift 3.2 / Alamofire 4.4)

  let manager = Alamofire.SessionManager.default manager.session.configuration.timeoutIntervalForRequest = 4 manager.request(.POST, CONSTANTS.APIEndpoint+"/test", parameters: parameters).responseJSON { (req, res, json, error) in if let json = self.handleAPIResponse(req, res: res, json_data: json, error: error){ }