9

Would anyone know what are the timeouts configuration for [NSURLSession sharedSession]?

I am doing some debugging and this information would be very helpful.

Edit

Apparently my question is not clear (!). I am not asking how to change the timeout. I am asking what the default is. So this is not a duplicate.

vib
  • 2,254
  • 2
  • 18
  • 36
  • 1
    Possible duplicate of [NSURLSession: How to increase time out for URL requests?](https://stackoverflow.com/questions/23428793/nsurlsession-how-to-increase-time-out-for-url-requests) – mfaani Sep 06 '17 at 22:07
  • 1
    According to the documentation, with the default configuration, `NSURLSession` will wait up to 60 seconds for new data to arrive, and will allow the entire operation to go for up to 7 days (assuming there's new data at least every 60 seconds). – Lily Ballard Sep 06 '17 at 22:30
  • @KevinBallard where is this documentation? – vib Sep 07 '17 at 07:07
  • 2
    `[[[NSURLSession sharedSession] configuration] timeoutIntervalForRequest]` should give you the value you want. – Larme Sep 07 '17 at 07:59
  • @Larme I'd be happy to accept your comment if you write it as an answer – vib Sep 07 '17 at 10:06

2 Answers2

24

There are two timeouts for URL sessions. The first is the maximum time allowed between receiving new data. This is called the timeoutIntervalForRequest. The second is the maximum time the entire request is allowed to take (assuming it's regularly receiving new data). This is called the timeoutIntervalForResource.

Both of these timeouts are configured by default using NSURLSessionConfiguration, and can be overridden on the NSURLRequest.

The default timeoutIntervalForRequest is 60 seconds.

The default timeoutIntervalForResource is 7 days.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • Its not woking for me and the timeout error comes in almost 30 second. – Imran Jan 03 '19 at 06:09
  • Even if I try to increase the timeoutIntervalForRequest to 120 it is still showing 60 in console. – Imran Jan 03 '19 at 06:18
  • I must be misunderstanding what it is for, if it makes zero sense to me that it should take 7 days(!) to time out..? –  Aug 12 '20 at 00:28
  • The default `timeoutIntervalForResource` is 7 days because of background URL sessions, where the network request may be blocked for an arbitrary amount of time (e.g. if I'm in power save or have no network, background network requests don't happen). For foreground URL sessions having an absurdly long timeout doesn't matter, the app is unlikely to be continuously foregrounded for 7 days straight. – Lily Ballard Aug 14 '20 at 22:09
3

There are three timeout URL sessions now. Following are the details:

  1. timeoutInterval - The connection timeout is the timeout in making the initial connection i.e. completing the TCP connection handshake. If during a connection attempt the request remains idle for longer than the timeout interval, the request is considered to have timed out. The default timeout interval is 60 seconds.

  2. timeoutIntervalForRequest - The timeout interval to use when waiting for additional data to arrive before giving up. This value is reset once new data arrive. The default value is 60.

  3. timeoutIntervalForResource - Any upload or download tasks created by a background session are automatically retried if the original request fails due to a timeout. To configure how long an upload or download task should be allowed to be retried or transferred, use the timeoutIntervalForResource property. The default value is 7 days.

Refer apple documentation for more details: timeoutInterval, timeoutIntervalForRequest and timeoutIntervalForResource

yo2bh
  • 1,356
  • 1
  • 14
  • 26