Reputation: 1750
I have been noticing a strange behavior on my app related to NSURLSession.
I use NSURLSession with a background configuration to start a download, the download starts without any problem, but at some point, apparently random, it stops for a few seconds, and restarts without calling any delegate other than
-URLSession: downloadTask: didWriteData: totalBytesWritten: totalBytesExpectedToWrite:
with a resetted number of bytes.
Can anyone tell me what's happening?
Here is how I initialize the session
if([[[UIDevice currentDevice]systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending)
{
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:sessionId];
}
else
{
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:sessionId];
}
sessionConfiguration.HTTPMaximumConnectionsPerHost = kSimultaneousDownloads;
[sessionConfiguration setSessionSendsLaunchEvents:YES];
sessionConfiguration.discretionary = NO;
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:self.backgroundQueue];
--EDIT--
I followed Rob's suggestions and using Charles and the xCode debbuger I found out that the order things happen are:
getTasksWithCompletionHandler
, there are no tasks on the session URLSession:didReceiveChallenge:completionHandler:
is called, I call completion handler completionHandler(NSURLSessionAuthChallengePerformDefaultHandling,nil);
didWriteData:
delegate shows download task with id 1 is downloading data, download shows on Charles with status Receiving response bodyURLSession:didReceiveChallenge:completionHandler:
is called, I call completion handler completionHandler(NSURLSessionAuthChallengePerformDefaultHandling,nil);
didWriteData:
delegate shows download task with id 2 is downloading data, the new download shows on Charles with status Receiving response bodyUpvotes: 2
Views: 1067
Reputation: 437422
A couple of suggestions:
Do you have any redirects or auth challenges involved here? Have you implemented those delegates to see if anything interesting is happening there?
Have you tried watching this with Charles to see what is actually going on with the connection?
This can be useful in diagnosing when the behavior is due to something actually happening in the connection versus some symptom resulting from our client-side code.
Finally, is your didWriteData
logging the task id (because with background sessions it can get confusing because a download initiated in a previous run of the app can finish up the next time you run the app).
Personally this last issue is the one that got me the most when I first started using background sessions. We're so accustomed to running app in the debugger, stopping, and re-running the app later with little residual effect of our prior session. But background sessions are vexing, because those old requests stay around unless you actually remove the app from the device/simulator.
Upvotes: 2