快速2.1 alamofire超时方法 [英] swift 2.1 alamofire timeout method

查看:127
本文介绍了快速2.1 alamofire超时方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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 , 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 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).

推荐答案

基于 NSURLSessionConfiguration类参考


此属性根据此配置确定会话中所有任务
的请求超时间隔。请求超时
的时间间隔控制任务在放弃之前应等待
个额外数据到达的时间(以秒为单位)。每当有新数据到达时,与
关联的计时器就被重置。当请求计时器
到达指定的间隔而没有接收到任何新数据时,它
会触发超时。

This property determines the request timeout interval for all tasks within sessions based on this configuration. The request timeout interval controls how long (in seconds) a task should wait for additional data to arrive before giving up. The timer associated with this value is reset whenever new data arrives. When the request timer reaches the specified interval without receiving any new data, it triggers a timeout.

每当有新数据到达时,计时器都会重置,这就是为什么您有不同的错误时间。

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超时方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆