Right Way for changing timeoutIntervalForRequest in Alamofire

对着背影说爱祢 提交于 2020-02-15 08:38:14

问题


I changed timeoutIntervalForRequest with

let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 3    
manager.request(url).response {}

but seems not worked, Any suggestion?


回答1:


This worked for me :)

let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = TimeInterval(7)
configuration.timeoutIntervalForResource = TimeInterval(7)
let session = URLSession(configuration: configuration)

let task = session.dataTask(with: url) {}

Changes to the configuration of an already initialized URLSession have no effect, per Apple's documentation. If you want a custom timeout, either set the value on your requests, or create a custom URLSession with a custom configuration, as outlined in document




回答2:


You could do something like:

lazy var alamofireSession : Alamofire.SessionManager = { [weak self] in
    let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForRequest = TimeInterval(30)
    configuration.timeoutIntervalForResource = TimeInterval(30)
    let session = Alamofire.SessionManager(configuration: configuration, delegate: sessionDelegate, serverTrustPolicyManager: nil)   
    return session
}()



回答3:


For swift3 try this

  var alamoManager: SessionManager?
    //Mark -- Alamofire session manager
                let configuration = URLSessionConfiguration.default
                configuration.timeoutIntervalForRequest = 3
                alamoManager = Alamofire.SessionManager(configuration: configuration)
alamoManager.request(request).responseJSON {
                        response in

            }

Also this is one option

Alamofire.SessionManager.default.session.configuration.timeoutIntervalForRequest = 300



回答4:


Try this

let request = NSMutableURLRequest(url: NSURL.init(string: "URL")! as URL)
request.timeoutInterval = 3
    // do additional configuration with your request like pass header etc....
Alamofire.request(request as! URLRequestConvertible).responseJSON {
        response in
        // do whatever you want here
    }


来源:https://stackoverflow.com/questions/43933708/right-way-for-changing-timeoutintervalforrequest-in-alamofire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!