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

- 259,804
- 351
- 777
- 1,080
-
2Set a breakpoint at the `test` function and print the `self.alamoFireManager!.session.configuration.timeoutIntervalForRequest` – Bannings Jun 30 '15 at 06:23
-
Are you assuming that the first time you call the method test(), it should have a 4 second delay? – Orion Jul 02 '15 at 07:31
-
The delay is for the http request not for the complete test method. For example the timeout dose not apply to your handleAPIResponse call. – alinoz Jul 02 '15 at 07:49
-
were you able to solve this? – Varun Singh Aug 20 '16 at 22:05
-
What is the use of 'center' object you created? – Jayprakash Dubey Dec 14 '16 at 12:54
6 Answers
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 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})
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.

- 2,822
- 22
- 38
-
1Swift 3 / April 2017: should be `Alamofire.SessionManager`, and `URLSessionConfiguration.default` – xaphod Apr 12 '17 at 16:38
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)

- 199
- 2
- 7
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)

- 9,114
- 6
- 43
- 48
-
In case of Alamofire 4.0 Alamofire.Manager has been changed to Alamofire.SessionManager as indicated by @Jamie – Supratik Majumdar Oct 19 '16 at 10:29
-
not wokring to me. Can you provide code for newest alamofire version? I got : "Alamofire.SessionManager(configuration: <#T##URLSessionConfiguration#>, delegate: <#T##SessionDelegate#>, serverTrustPolicyManager: <#T##ServerTrustPolicyManager?#>)" – famfamfam Nov 22 '18 at 10:28
I guess this one works for you, note that you have to declare instance of Alamofire.Manager outside the function, please see below code
//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
}
}
hope this helps you

- 2,141
- 1
- 23
- 30
-
i can confirm that this will not work if the instance of alamofire manager is declared inside the function. – Andrew Mar 10 '17 at 21:48
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){
}
}
}
Use self.alamoFireManager!.request
in the same init function , timeout delay is only for the request not for the whole function. may be it can help.

- 277
- 4
- 17
I got NSURLErrorDomain when I used above method . This is my code (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){
}

- 156
- 1
- 5