0

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;
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
mshrestha
  • 788
  • 5
  • 14
  • BTW, if your resource timeouts are so short, it begs the question of why you’re using background session at all. We usually only go for all the background session overhead when we've got requests that can _not_ be completed within 30 seconds (i.e., the amount of time that you can [extend your app’s background execution](https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/extending_your_app_s_background_execution_time?language=objc)). – Rob Oct 16 '22 at 20:53
  • @Rob Thanks for getting back. Trying to get back on all your comments. Yes I tried the timeoutInterval for NSMutableURLRequest and that does not trigger didCompleteWithError. From few online searches seems like setting timeoutIntervalForResource is the way to go from iOS 8 and later. – mshrestha Oct 16 '22 at 22:03
  • @Rob I was not sure if having multiple session was a good approach but its good to know that is an option as well. Also, those timeouts were just an arbitrary numbers :). But any clue as to why setting timeoutIntervalForRequest for config triggers didCompleteWithError but self.session.configuration.timeoutIntervalForResource = 20.0f does not? – mshrestha Oct 16 '22 at 22:07

1 Answers1

0

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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • this makes sense why it does not trigger the delegate methods when setting the values the second way. But this actually limits the way we would be able to set the timeoutIntervalForResource for the session's configuration depending on different task since we can only set background configuration once for our session with that specific identifier – mshrestha Oct 16 '22 at 23:55
  • “But this actually limits the way we would be able to set ...” - Perhaps it does, but it is obviously a conscious design choice on their part. You could always submit a feature request with the “[Feedback Assistant](https://developer.apple.com/bug-reporting/)”, but don’t be surprised if you get a generic ”functions as designed” reply. Regardless, good luck and happy coding. – Rob Oct 17 '22 at 01:08