Setting client-side timeout per request with Alamofire [swift]?

Troy Payne picture Troy Payne · Nov 25, 2014 · Viewed 12.4k times · Source

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.

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

Answer

user3786678 picture user3786678 · Dec 2, 2014

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

    }
}