【问题标题】:AlamoFire does not respect timeout intervalAlamoFire 不遵守超时间隔
【发布时间】:2015-09-14 16:47:45
【问题描述】:
class APIClient {
    var user = User()
    let alamoFireManager : Alamofire.Manager?
    let center = NSNotificationCenter.defaultCenter()


    init(){
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.timeoutIntervalForRequest = 4 // seconds
        configuration.timeoutIntervalForResource = 4
        self.alamoFireManager = Alamofire.Manager(configuration: configuration)
    }

    func test(){
        //This does not respect the 4 second time out. Why?
        self.alamoFireManager!.request(.POST, CONSTANTS.APIEndpoint+"/test", parameters: parameters).responseJSON {
                        (req, res, json, error)  in
                        if let json = self.handleAPIResponse(req, res: res, json_data: json, error: error){
                        }
                    }
    }

【问题讨论】:

  • test函数处设置断点并打印self.alamoFireManager!.session.configuration.timeoutIntervalForRequest
  • 你是不是假设第一次调用test()方法应该有4秒的延迟?
  • 延迟是针对http请求的,不是针对完整测试方法的。例如,超时不适用于您的 handleAPIResponse 调用。
  • 你能解决这个问题吗?
  • 你创建的'center'对象有什么用?

标签: ios swift alamofire


【解决方案1】:

您确定没有强制执行 4 秒超时吗?我创建了一个实验:

    let center = NSNotificationCenter.defaultCenter()
    var alamoFireManager : Alamofire.Manager?

    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.timeoutIntervalForRequest = 4 // seconds
    configuration.timeoutIntervalForResource = 4
    self.alamoFireManager = Alamofire.Manager(configuration: configuration)

    self.alamoFireManager!.request(.POST, "http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/2", parameters: nil).responseJSON {
        (req, res, json, error)  in
        println("First json \(json)")
        println("First error \(error)")
    }

    self.alamoFireManager!.request(.POST, "http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/6", parameters: nil).responseJSON {
        (req, res, json, error)  in
        println("Second \(json)")
        println("Second \(error)")
    }

以及我得到的控制台输出:

First json Optional({
    name = timeoutTest;
    value = 2;
})
First error nil
Second nil
Second Optional(Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7f91dc8223e0 {NSErrorFailingURLKey=http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/6, NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/6})

我的示例中的 URL 当前已关闭,我将尝试将它们重新上线。 您可以使用示例中的 url 进行测试。通过在末尾设置不同的数字,您可以修改超时(以秒为单位)。

我使用了 cocoapods 最新版本的 Alamofire。

【讨论】:

  • Swift 3 / 2017 年 4 月:应该是 Alamofire.SessionManagerURLSessionConfiguration.default
【解决方案2】:

您需要为请求管理器创建一个全局变量:

var alamoFireManager = Alamofire.Manager.sharedInstance

配置自定义参数后:

 let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.timeoutIntervalForRequest = 4 // seconds
    configuration.timeoutIntervalForResource = 4
    self.alamoFireManager = Alamofire.Manager(configuration: configuration)

【讨论】:

    【解决方案3】:

    这是 Swift 3.0 / Alamofire 4.0 代码,用于获取具有 5 秒超时的 alamofireManager:

        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForResource = 5 // seconds
    
        let alamofireManager = Alamofire.SessionManager(configuration: configuration)
    

    【讨论】:

    • 在 Alamofire 4.0 的情况下,Alamofire.Manager 已更改为 Alamofire.SessionManager,如 @Jamie 所示
    • 不适合我。你能提供最新alamofire版本的代码吗?我得到:“Alamofire.SessionManager(配置:,委托:,serverTrustPolicyManager:)”
    【解决方案4】:

    我猜这个对你有用,注意你必须在函数之外声明 Alamofire.Manager 的实例,请看下面的代码

    //Declare it out side the function
    var alamoFireManager : Alamofire.Manager!
    
    func callUrl() {
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.timeoutIntervalForResource = 10800 // seconds
        configuration.timeoutIntervalForRequest = 10800 // seconds
    
        alamoFireManager = Alamofire.Manager(configuration: configuration)
    
        alamoFireManager.request(.POST, url, parameters: dir as? [String : AnyObject], encoding: .JSON, headers: [ "Content-Type": "application/json"])
            .responseJSON { response in
        }
    }
    

    希望对你有帮助

    【讨论】:

    • 我可以确认,如果在函数内部声明了 alamofire 管理器的实例,这将不起作用。
    【解决方案5】:
     init(){
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.timeoutIntervalForResource = 4
        configuration.timeoutIntervalForRequest = 4
        self.alamoFireManager = Alamofire.Manager(configuration: configuration)
        self.alamoFireManager!.request(.POST, CONSTANTS.APIEndpoint+"/test", parameters: parameters).responseJSON {
                        (req, res, json, error)  in
                        if let json = self.handleAPIResponse(req, res: res, json_data: json, error: error){
                        }
                    }
    }
    

    在同一个init函数中使用self.alamoFireManager!.request,超时延迟只针对请求,不针对整个函数。可能会有帮助。

    【讨论】:

    • 你能提供最新 alamofire 版本的代码吗? Tks
    【解决方案6】:

    当我使用上述方法时,我得到了 NSURLErrorDomain。 这是我的代码(swift 3.2/Alamofire 4.4)

        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 4
    
        manager.request(.POST, CONSTANTS.APIEndpoint+"/test", parameters: parameters).responseJSON {
                        (req, res, json, error)  in
                        if let json = self.handleAPIResponse(req, res: res, json_data: json, error: error){
                        }
    

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 2020-07-18
      • 2018-03-30
      • 1970-01-01
      • 2013-03-24
      相关资源
      最近更新 更多