5

Is it possible to set different expiration timeout for each request?

The only way I found was to create a new NSURLSession with a different NSURLSessionConfiguration and change the timeoutIntervalForResource. Same thing with frameworks like Alamofire.

Luca Torella
  • 7,974
  • 4
  • 38
  • 48

2 Answers2

5

You absolutely can do this. You can set the value directly on an NSMutableURLRequest which can be passed into Alamofire.

let URL = NSURL(string: "https://httpbin.org/get")!
let mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.timeoutInterval = 5.0

Alamofire.request(mutableURLRequest)
    .responseJSON { response in
        debugPrint(response)
    }

UPDATE

You cannot control the timeoutIntervalForResource per request. The timeout interval property on NSURLRequest is equivalent to timeoutIntervalForRequest on the NSURLSessionConfiguration. More details here.

cnoon
  • 16,575
  • 7
  • 58
  • 66
  • If I'm not wrong, `timeoutInterval` is the equivalent of `NSURLSessionConfiguration` `timeoutIntervalForResource`. Is it possible to change the `timeoutIntervalForRequest` per request? – Luca Torella Oct 19 '15 at 07:39
  • sorry I mean the other way around. `timeoutIntervalForRequest` can be set per request with `timeoutInterval `, but `timeoutIntervalForResource` which is the one I need can't – Luca Torella Oct 19 '15 at 08:12
  • Ah ic now. From what I can tell from all the docs, this is not possible to control per request. My answer has been updated accordingly. – cnoon Oct 20 '15 at 15:23
  • You can always do it with an external dictionary of dictionaries of timers, using the session and task identifier as keys, invalidating the timer when a task finishes, and canceling the task if the timer fires first. That was the recommended approach for a long time anyway, because the timers in NSURLConnection were so quirky. – dgatwood Dec 03 '15 at 22:49
-1

Yes, You can do this-

Use this code

[[[NSURLSession sharedSession] configuration] setTimeoutForResource:30];

You have to just change the setTimeForResource in different view controllers.

Avinash651
  • 1,399
  • 11
  • 17
  • 1
    but it will affect all requests in the queue. Let's suppose I've 9 requests in the queue and then when I add the 10th I change the timeout. This change will affect all of them, which is something I don't want – Luca Torella Oct 16 '15 at 12:04