NSURLSessionConfiguration and timeoutIntervalForResource for NSURLSessionDelegate didCompleteWithError

189 views Asked by At

Wondering if someone can provide their two cents. Below code successfully triggers didCompleteWithError NSURLSessionDelegate method upon timeoutIntervalForResource timeout.

NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.xxx.xxx"];
config.timeoutIntervalForRequest = 20.0f;
config.timeoutIntervalForResource = 20.0f;
self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

But setting the timeout like below does not

self.session.configuration.timeoutIntervalForResource = 20.0f;
self.session.configuration.timeoutIntervalForRequest = 20.0f;

The goal I am trying to achieve is to change the timeoutIntervalForResource depending on different API

if(needs less timeout){
   self.session.configuration.timeoutIntervalForResource = 8.0f;
} else{
   self.session.configuration.timeoutIntervalForResource = 30.0f;
}
1

There are 1 answers

2
Rob On

You said:

Below code successfully triggers didCompleteWithError NSURLSessionDelegate method upon timeoutIntervalForResource timeout.

NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.xxx.xxx"];
config.timeoutIntervalForRequest = 20.0f;
config.timeoutIntervalForResource = 20.0f;
self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

But setting the timeout like below does not

self.session.configuration.timeoutIntervalForResource = 20.0f;
self.session.configuration.timeoutIntervalForRequest = 20.0f;

You are attempting to change the configuration settings after the session was instantiated. You must adjust the configuration settings before the session is instantiated. As the docs say (emphasis added):

Beginning in iOS 9 and OS X 10.11, NSURLSession objects store a copy of the NSURLSessionConfiguration object passed to their initializers, such that a session’s configuration is immutable after initialization. Any further changes to mutable properties on the configuration object passed to a session’s initializer or the value returned from a session’s configuration property do not affect the behavior of that session. However, you can create a new session with the modified configuration object.