AlamoFire does not respect timeout interval AlamoFire does not respect timeout interval swift swift

AlamoFire does not respect timeout interval


Are you sure that the 4 seconds timeout is not enforced? I have created an experiment:

    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)")    }

and the console output i got:

First json Optional({    name = timeoutTest;    value = 2;})First error nilSecond nilSecond 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})

The URLs from my example are currently down, i will try to put them back online. You can use the urls from the example to test. By setting a different number at the end you can modify the timeout (in seconds).

I have used the cocoapods last version of Alamofire.


You need create a global variable for the request manager:

var alamoFireManager = Alamofire.Manager.sharedInstance

And after configure the custom parameters:

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


Here's the Swift 3.0 / Alamofire 4.0 code to get an alamofireManager that has a 5 second timeout:

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