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?

Replies

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"
  • Hi eskimo, first of all thank you for all the info that you provide replying to questions. Here you are saying that it's possible to create a resource timeout with NSTimer, especially for the background session case. But I cannot understand why it's needed. Are you able to elaborate on that? I'm trying to fix a quite strange problem I have when the application enters background (see https://developer.apple.com/forums/thread/696762?page=1#698926022). Waiting for a reply I thank you for your attention. All the best, Lorenzo

Add a Comment

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"