Alamofire:不会根据配置更改SessionManager的超时

时间:2018-10-16 23:18:18

标签: swift alamofire

这是我的代码在尝试设置SessionManager时尝试更改超时时间:

private var defaultSessionManager: Alamofire.SessionManager = {
    let config = URLSessionConfiguration.default
    config.httpMaximumConnectionsPerHost = 10
    config.timeoutIntervalForRequest = TimeInterval(120)
    return Alamofire.SessionManager(configuration: config)
}()

但是,请求的超时总是60秒(这是iOS工具netfox的屏幕截图):

Netfox screenshot of a request

我也尝试将timeoutIntervalForResource配置为也没有成功。
这不是通过SessionManager为请求配置超时长度的正确方法吗?如果是这样,我该怎么做

1 个答案:

答案 0 :(得分:0)

您需要同时设置timeoutIntervalForRequesttimeoutIntervalForResource  使它按预期工作。这是我目前正在使用的,

extension SessionManager {

    /// A `SessionManager` to have a request timeout of 2 minutes.
    static let customDelayed: SessionManager = {
        let secondsInOneMinute = 60
        let numberOfMinutesForTimeout = 2
        let timoutInterval = TimeInterval(numberOfMinutesForTimeout * secondsInOneMinute)

        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = timoutInterval
        configuration.timeoutIntervalForResource = timoutInterval
        return SessionManager(configuration: configuration)
    }()
}