NSURLSession - Different timeouts per task?

Trying to convert some code over from NSURLConnection and noticed that the timeouts are per-session, not per-task. In our existing code there are a few places where some requests are given longer timeouts then the others.


I see that in NSURLSession the timeouts are all session based. i.e one-size-fits all. There seem to be a couple of options to be able to fine-tune the timouts per-task:


  1. Use different sessions for each different timeout. Not pleasant and goes against the idea of having one session-per client-server pairing.
  2. Set the Session timeout for each task at task creation time. Not even sure this would work and if it did would need additional multithreading safeguards to ensure only one task at at time can be created.


Anyone else had to deal with this oversight?

If you set a specific timeout on the request (via NSURLRequest’s

timeoutInterval
property) then it’ll be honoured by NSURLSession.

IMPORTANT The

timeoutInterval
property is equivalent to
timeoutIntervalForRequest
property. NSURLRequest has no equivalent to
timeoutIntervalForResource
, although that shouldn’t be a problem because you’re coming from NSURLConnection and it didn’t support that logic. Moreover, if you do need a resource timeout you can implement it with NSTimer; the resource timeout is necessary with NSURLSession because, in the background session case, your code might not be running.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks - So can you confirm that any tasks created from an NSURLRequest (e.g [NSURLSession dataTaskWithRequest:]) will use that request's timeout, regardless of whether the NSURLRequest was created without explicitly specifying a timeout interval (with a default 60 seconds) or using any of the explicit initializers that take a timeoutInterval:.


Whilst presumably any of the NSURLSession that create a task via other means (e.g [NSURLSession dataTaskWithURL:]) have a timeout based on NSURLSession.timeoutIntervalForRequest. I am guessing that internally they just create a NSURLRequest and set its timeout to the current value of timeoutIntervalForRequest


We never had the equivalent of timeoutIntervalForResource before, so can live without being able to control that per-task.

So can you confirm that any tasks created from an NSURLRequest (…) will use that request's timeout, regardless of whether the NSURLRequest was created without explicitly specifying a timeout interval (with a default 60 seconds) or using any of the explicit initializers that take a timeoutInterval:.

That’s not how things currently work. Rather, NSURLSession treats 60.0 as the default value. If the request has a

timeoutInterval
property value of 60.0, regardless of how it got it, that request will use
timeoutIntervalForRequest
from the session.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
NSURLSession - Different timeouts per task?
 
 
Q