I have a question about alamofire timeout methods, first of all, my English may not be good enough for you guys to understand what I said ... but I will get tired of explaining my question.
in my project, I used alamofire, for some reason I need to make sure my application works in a bad connection area. so I think using the timeout method.
I saw some say, use Solution 1:
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.timeoutIntervalForRequest = 2 // seconds self.alamofireManager = Alamofire.Manager(configuration: configuration) self.alamofireManager!.request(.GET, "http://example.com/") .response { (request, response, data, error) in
will solve the problem, but in my case I saw that the debug area said that my request would “cancel”
so I tried another solution that I saw here (stackoverflow) Solution 2:
let manager = Alamofire.Manager.sharedInstance manager.session.configuration.timeoutIntervalForRequest = 5 manager.request(.POST, url, parameters: params, encoding: .JSON, headers: nil).response(queue: dispatch_get_main_queue()) { (Request, res, data, error) -> Void in{}
this method seems to work, but only if I let my application switch to the background and respond to my application, the debug area will display a timeout request, otherwise the debug area will not show anything unless I switch it to background and reactive Appendix.
I am using Xcode 7.1 and ios 9.0
UPDATE: if I used solution 2, if I allow requesttimeout = 5, the debug area will show a “timeout error”, but it will take more than 5 seconds ... sometimes it will be displayed for about 30 seconds, but sometimes more than 1 minute ...
UPDATE2: I found out what the problem is. the problem is that if you use the reachability infrastructure to detect the status of the connection, then the system may detect that the connection is enabled or not at the beginning, then it will display the request timeout after isReachable (this can take up to 1 minute).