Himan Dhawan
Himan Dhawan

Reputation: 924

Session Configuration Time Out interval not able to set greater than 60 seconds

In my application data that I fetch from server is quite heavy and is coming as Bulk Data in single API. Due to which I increased my Time Out Interval to 1800 seconds. My code is

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.mobiletuts.Singlecast.BackgroundSession"];
        // Session Configuration
        [sessionConfiguration setTimeoutIntervalForRequest:30*60];
        // Initialize Session
        session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

NSMutableURLRequest* request = [[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:fdi.downloadSource]] mutableCopy];
    request = [CommonFunctions AddRequestHeaders:request];
    [request setTimeoutInterval:30*60];
    fdi.downloadTask = [self.session dataTaskWithRequest:request];
    [fdi.downloadTask resume];

Even my session is showing timeout 1800 seconds but I get request time out after 60 seconds. How can I increase the waiting time so that we should not get request timeout?

Upvotes: 0

Views: 415

Answers (1)

dgatwood
dgatwood

Reputation: 10417

Short answer? You can't, and you shouldn't try.

When an iOS device is waiting for data with an open connection to your server, its network hardware (Wi-Fi or cellular) is powered up, using precious battery power. For this reason, there are limits to how long you can keep that hardware hot while waiting for data to arrive.

In your case, your server isn't sending out even one single byte of data for more than an entire minute. It just isn't reasonable to expect an HTTP/HTTPS request to stay alive through such an extended period of inactivity.

Further, over a cellular connection, the odds of actually receiving data approach zero as the connection duration increases, because you can switch to a different tower, switch from 3G to LTE or vice-versa, experience a period of massive packet loss that causes a connection failure, etc. So even if it weren't a huge waste of battery power and a huge waste of shared cellular tower bandwidth, it still wouldn't be particularly practical to keep a cellular link going while it isn't passing any data.

So if your server really is going for 60+ seconds without sending a single byte of data, then you need to completely rethink the way you're doing things, and split the request into two calls:

  • Generate data — Tell the server to start processing the data to make it ready for your app to download.
  • Retrieve data — Ask the server if the data is ready yet, and if so, retrieve it (and, optionally, purge it, or do that via a separate request).

Make the first request, then make the second request periodically (say once per minute) until it actually returns the data.

Don't forget to add a server-side script of some sort to purge any generated data that has not been claimed after some period of time (a day, a week, whatever), so that it doesn't just pile up forever.

If you aren't expecting your server to not send any data for such a long period of time, then you should spend some time figuring out what's wrong on the server side.

Upvotes: 1

Related Questions