使用Alamofire [swift]设置每个请求的客户端超时?

时间:2022-10-14 21:54:10

I'm trying to set a client-side timeout per request for Alamofire for Swift. The lead architect told me to set this on NSURLRequest, but I'm completely confused on how to actually do that in practice.

我试图为Swift的Alamofire设置每个请求的客户端超时。首席架构师让我将其设置为NSURLRequest,但我完全不知道如何在实践中实现这一点。

Can someone who has done this give an example? Thanks!

有人能举个例子吗?谢谢!

2 个解决方案

#1


17  

I think this code may works.

我认为这个代码可能有用。

var alamofireManager : Alamofire.Manager?

func some(){
  let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
  configuration.timeoutIntervalForResource = 2 // seconds

  self.alamofireManager = Alamofire.Manager(configuration: configuration)
  self.alamofireManager!.request(.GET, "http://example.com/")
    .response { (request, response, data, error) in

    }
}

#2


0  

This is how you can use a per-request timeout using URLRequest:

这就是如何使用URLRequest的每个请求超时:

Alamofire.request(URLRequest(url: ..., cachePolicy: ..., timeoutInterval: 10))
    .response(...)

#1


17  

I think this code may works.

我认为这个代码可能有用。

var alamofireManager : Alamofire.Manager?

func some(){
  let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
  configuration.timeoutIntervalForResource = 2 // seconds

  self.alamofireManager = Alamofire.Manager(configuration: configuration)
  self.alamofireManager!.request(.GET, "http://example.com/")
    .response { (request, response, data, error) in

    }
}

#2


0  

This is how you can use a per-request timeout using URLRequest:

这就是如何使用URLRequest的每个请求超时:

Alamofire.request(URLRequest(url: ..., cachePolicy: ..., timeoutInterval: 10))
    .response(...)