ios AlamoFire 不遵守超时间隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31095070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:37:34  来源:igfitidea点击:

AlamoFire does not respect timeout interval

iosswiftalamofire

提问by TIMEX

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){
                        }
                    }
    }

回答by alinoz

Are you sure that the 4 seconds timeout is not enforced? I have created an experiment:

您确定没有强制执行 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)")
    }

and the console output i got:

和我得到的控制台输出:

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})

The URLs from my example are currently down, i will try to put them back online. You can use the urls from the example to test. By setting a different number at the end you can modify the timeout (in seconds).

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

I have used the cocoapods last version of Alamofire.

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

回答by Dab88

You need create a global variable for the request manager:

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

var alamoFireManager = Alamofire.Manager.sharedInstance

And after configure the custom parameters:

并在配置自定义参数后:

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

回答by Jamie McCrindle

Here's the Swift 3.0 / Alamofire 4.0 code to get an alamofireManager that has a 5 second timeout:

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

    let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForResource = 5 // seconds

    let alamofireManager = Alamofire.SessionManager(configuration: configuration)

回答by Patel Jigar

I guess this one works for you, note that you have to declare instance of Alamofire.Manageroutside the function, please see below code

我猜这个对你有用,注意你必须在函数外声明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
    }
}

hope this helps you

希望这对你有帮助

回答by Faran Ghani

 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){
                    }
                }
}

Use self.alamoFireManager!.requestin the same init function , timeout delay is only for the request not for the whole function. may be it can help.

使用self.alamoFireManager!.request在相同的初始化函数,超时时间只对请求不是针对整个功能。也许它可以提供帮助。

回答by Leibniz

I got NSURLErrorDomain when I used above method . This is my code (swift 3.2/Alamofire 4.4)

当我使用上述方法时,我得到了 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){
                    }