问题描述
我对alamofire超时方法有疑问,首先是
,我的英语水平可能不够好,无法让你们听懂我说的话...但是我会厌倦解释我的问题
i have question about alamofire timeout methods,first of all , my english may not be good enough to let you guys to understand what i said... but i will tired to explain my question
在我的项目中,出于某种原因,我使用了alamofire,我需要确保我的应用程序的连接区域不好。所以我在考虑使用超时方法。
in my project, i used alamofire , for some reason , i need to make sure my app working of poor connection area. so i am thinking using timeout method.
我看到有人说,使用
解决方案1:
i saw there are some people said , useSolution 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 solved the problem, but in my case , i saw the debug area said my request is "cancel"
所以我尝试了在这里看到的另一个解决方案(堆栈溢出)
解决方案2:
so i tried another solution which i saw on 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 should be working , but only if i let my app went to background and reactive my app ,the debug area show request "timeout", otherwise the debug area didnt show anything ,unless i switch it to background and reactive the app.
I' m使用Xcode 7.1和ios 9.0
I'm using Xcode 7.1 and ios 9.0
更新:
如果我使用解决方案2,如果我让requesttimeout = 5,则调试区域将显示超时错误,但需要5秒钟以上的时间...有时它将显示30秒左右,但有时超过1分钟...。
UPDATE:if i used Solution 2 , if i let the requesttimeout = 5 , the debug area will show the "timeout error", but it take more than 5 sec... sometimes it will show around 30s, but sometimes more than 1 min....
UPDATE2:
i找出了问题所在。问题是,如果您使用的是reachabilty框架来检测连接状态,则系统可能会首先检测到连接是否打开,然后在isReachable之后可能会显示请求超时(最多可能需要1分钟)。
UPDATE2 :i found out what the problem is. the problem is if you are using reachabilty framework to detect connect status, then the system might detect the connect is on or not first , then it will show the request timeout after the isReachable( it may take up to 1 min).
推荐答案
基于
每当有新数据到达时,计时器都会重置,这就是为什么您有不同的错误时间。
Timer is reset whenever new data arrives and that is why you have different error time.
也可以为请求设置另一个参数:
Also you could set one more parameter for the request :
manager.session.configuration.timeoutIntervalForResource = 5
这篇关于快速2.1 alamofire超时方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!