Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

ios - AlamoFire does not respect timeout interval

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){
                        }
                    }
    }
question from:https://stackoverflow.com/questions/31095070/alamofire-does-not-respect-timeout-interval

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

I have used the cocoapods last version of Alamofire.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...