Set timeout in Alamofire

后端 未结 14 1150
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 03:33

I am using Alamofire 4.0.1 and I want to set a timeout for my request. I tried the solutions gived in this question:

In the first case

相关标签:
14条回答
  • 2020-12-01 04:15

    i have code for swift 2.3 hope it helps you, try it

        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.timeoutIntervalForResource = 10800 // seconds
        configuration.timeoutIntervalForRequest = 10800 // seconds
    
        alamoFireManager = Alamofire.Manager(configuration: configuration)
    
    0 讨论(0)
  • 2020-12-01 04:16

    None of the above worked for me: Im on swift 4.2 Alamofire 4.5

    I managed to solve it like this :

    let request = Alamofire.request("routee", method: .post, parameters: data, encoding: JSONEncoding.default, headers: getHeaders())
    
    /// getting request created by Alamofire and then updating its timeout Value
    
    let url = URL(string: "myroute")!
            var request = try URLRequest(url: url, method: method, headers: headers)
            request.timeoutInterval = 900 // timeout
            request = try JSONEncoding.default.encode(request, with: data)
    
    Alamofire.request(request)
        .responseJSON { response in
    
    }
    
    0 讨论(0)
提交回复
热议问题