Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set timeout in Alamofire

I am using Alamofire 4.0.1 and I want to set a timeout for my request. I tried the solutions gived in this question:

In the first case, it throws a NSURLErrorDomain (timeout is set correctly):

let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForRequest = 10      let sessionManager = Alamofire.SessionManager(configuration: configuration)     sessionManager.request("yourUrl", method: .post, parameters: ["parameterKey": "value"])             .responseJSON {                 response in                 switch (response.result) {                 case .success:                     //do json stuff                     break                 case .failure(let error):                     if error._code == NSURLErrorTimedOut {                         //timeout here                     }                     print("\n\nAuth request failed with error:\n \(error)")                     break                 }             } 

In the second case, the time out is not replaced and still set as 60 seconds.

let manager = Alamofire.SessionManager.default manager.session.configuration.timeoutIntervalForRequest = 10  manager.request("yourUrl", method: .post, parameters: ["parameterKey": "value"]) 

I am running in ios 10.1

My code: (it doesn't work)

    let configuration = URLSessionConfiguration.default     configuration.timeoutIntervalForRequest = 10 // seconds     configuration.timeoutIntervalForResource = 10     let alamoFireManager = Alamofire.SessionManager(configuration: configuration)      alamoFireManager.request("my_url", method: .post, parameters: parameters).responseJSON { response in           switch (response.result) {         case .success:                  //Success....             break         case .failure(let error):             // failure...             break         }     } 

Solved Alamofire github thread: Alamofire 4.3.0 setting timeout throws NSURLErrorDomain error #1931

like image 551
ƒernando Valle Avatar asked Jan 23 '17 10:01

ƒernando Valle


2 Answers

For new versions of AF, see https://stackoverflow.com/a/61192412/308315


For old versions, please try this:

    let request = NSMutableURLRequest(url: URL(string: "")!)     request.httpMethod = "POST"     request.setValue("application/json", forHTTPHeaderField: "Content-Type")     request.timeoutInterval = 10 // 10 secs     let values = ["key": "value"]     request.httpBody = try! JSONSerialization.data(withJSONObject: values, options: [])     Alamofire.request(request as! URLRequestConvertible).responseJSON {         response in         // do whatever you want here     } 
like image 62
Kamal Thakur Avatar answered Sep 21 '22 04:09

Kamal Thakur


I have same problem too, I think I found the solution. Try to declare SessionManager?or in your case alamofireManager in class, outside the function

class ViewController: UIViewController {    var alamoFireManager : SessionManager? // this line     func alamofire(){         let configuration = URLSessionConfiguration.default         configuration.timeoutIntervalForRequest = 10         configuration.timeoutIntervalForResource = 10         alamoFireManager = Alamofire.SessionManager(configuration: configuration) // not in this line          alamoFireManager.request("my_url", method: .post, parameters: parameters).responseJSON { response in           switch (response.result) {         case .success:                  //Success....             break         case .failure(let error):                // failure...              break        }      }    }  } 
like image 41
Aldo Lazuardi Avatar answered Sep 19 '22 04:09

Aldo Lazuardi