Alamofire 忽略 timeoutIntervalForRequest 和 timeoutIntervalForResource 参数
Alamofire ignores timeoutIntervalForRequest and timeoutIntervalForResource parameters
我目前正在尝试为我的 iOS 应用程序中的每个 Alamofire 远程调用减少 timeoutIntervalForRequest
和 timeoutIntervalForResource
的默认值 NSURLSessionConfiguration
,因为我不这样做希望它分别等待 60 秒和 7 天。
这是我的代码:
Alamofire.Manager.sharedInstance.session.configuration.timeoutIntervalForRequest = 30
Alamofire.Manager.sharedInstance.session.configuration.timeoutIntervalForResource = 30
let parameters = [
"foo": [1,2,3],
"bar": [
"baz": "qux"
]
]
Alamofire.Manager.sharedInstance.request(.POST, "someURL", parameters: parameters, encoding: .JSON) {
(response) in
print(response.timeline)
}
这是我的日志:
Timeline: { "Latency": 60.977 secs, "Request Duration": 60.977 secs, "Serialization Duration": 0.000 secs, "Total Duration": 60.977 secs }
如您所见,Request Duration
值超过 30 秒。
这是否取决于我使用 Alamofire.Manager.sharedInstance.request
而不是 Alamofire.request
?
我很确定这是因为您无法更改默认管理器的配置(或者至少我认为您不能,这可能是一个错误)。
尝试实例化一个新的并添加自定义的 NSURLSessionConfiguration。
例如:
var manager:Alamofire.Manager?
func initManager(timeoutInterval:Double) {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = timeoutInterval
configuration.timeoutIntervalForResource = timeoutInterval
manager = Alamofire.Manager(configuration: configuration)
}
// then use manager!.request to do your request
我在官方 Alamofire GitHub 页面上打开了一个 issue,这是他们对此的看法:
Changing the values of the configuration in a session that's already active has no effect. According to Apple's documentation on NSURLSession
:
Changing mutable values within the configuration object has no effect on the current session, but you can create a new session with the modified configuration object.
I suggest you create your manager instance and customize its configuration, as per our documentation.
我目前正在尝试为我的 iOS 应用程序中的每个 Alamofire 远程调用减少 timeoutIntervalForRequest
和 timeoutIntervalForResource
的默认值 NSURLSessionConfiguration
,因为我不这样做希望它分别等待 60 秒和 7 天。
这是我的代码:
Alamofire.Manager.sharedInstance.session.configuration.timeoutIntervalForRequest = 30
Alamofire.Manager.sharedInstance.session.configuration.timeoutIntervalForResource = 30
let parameters = [
"foo": [1,2,3],
"bar": [
"baz": "qux"
]
]
Alamofire.Manager.sharedInstance.request(.POST, "someURL", parameters: parameters, encoding: .JSON) {
(response) in
print(response.timeline)
}
这是我的日志:
Timeline: { "Latency": 60.977 secs, "Request Duration": 60.977 secs, "Serialization Duration": 0.000 secs, "Total Duration": 60.977 secs }
如您所见,Request Duration
值超过 30 秒。
这是否取决于我使用 Alamofire.Manager.sharedInstance.request
而不是 Alamofire.request
?
我很确定这是因为您无法更改默认管理器的配置(或者至少我认为您不能,这可能是一个错误)。
尝试实例化一个新的并添加自定义的 NSURLSessionConfiguration。
例如:
var manager:Alamofire.Manager?
func initManager(timeoutInterval:Double) {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = timeoutInterval
configuration.timeoutIntervalForResource = timeoutInterval
manager = Alamofire.Manager(configuration: configuration)
}
// then use manager!.request to do your request
我在官方 Alamofire GitHub 页面上打开了一个 issue,这是他们对此的看法:
Changing the values of the configuration in a session that's already active has no effect. According to Apple's documentation on
NSURLSession
:Changing mutable values within the configuration object has no effect on the current session, but you can create a new session with the modified configuration object.
I suggest you create your manager instance and customize its configuration, as per our documentation.