Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request timeout is not working #1291

Closed
Dhvl-Golakiya opened this issue Jun 4, 2016 · 2 comments
Closed

Request timeout is not working #1291

Dhvl-Golakiya opened this issue Jun 4, 2016 · 2 comments
Assignees
Labels

Comments

@Dhvl-Golakiya
Copy link

Dhvl-Golakiya commented Jun 4, 2016

Hi,

I have set timeoutIntervalForRequest and timeoutIntervalForResource both for NSURLSessionConfiguration but service stop directly when I use below code.

func callService() {
       // var alamoFireManager : Alamofire.Manager?
var alamoFireManager = Alamofire.Manager.sharedInstance
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.timeoutIntervalForRequest = 10 // seconds
        configuration.timeoutIntervalForResource = 10

        alamoFireManager = Alamofire.Manager(configuration: configuration)
        alamoFireManager.request(.GET, RequestCall.API_URL + request, parameters: parameters, encoding: .URL)
            .responseJSON { response in

                if let JSON = response.result.value {
                    print(JSON)
                    callback!((JSON as! NSDictionary))
                }
        }
}

Is any wrong with code? I want timeout when response not retrieve after 10 seconds.

@Dhvl-Golakiya Dhvl-Golakiya changed the title Timeout is not working Request timeout is not working Jun 4, 2016
@jshier
Copy link
Contributor

jshier commented Jun 4, 2016

As soon as your code leaves callService(), there is no longer any reference to your alamoFireManager variable and it is deallocated, cancelling any pending requests you have. You're also redundantly setting alamoFireManager to the sharedInstance and then to a newly allocated instance later on.

What the documentation about custom Managers doesn't state is that you need to keep the manager around somehow. I like to use a static property in a struct:

struct APIManager {
    static let sharedManager: Manager = {
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.timeoutIntervalForRequest = 10        
        return Manager(configuration: configuration)
    }()
}

@jshier jshier self-assigned this Jun 4, 2016
@jshier jshier added the support label Jun 4, 2016
@jshier jshier closed this as completed Jun 4, 2016
@po0uyan
Copy link

po0uyan commented Jun 5, 2018

@jshier tnx you've saved my life

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants