I'm running Xcode 8.2, ios 10.2, iphone 7.
I have 2 NSURLSessions with background session configs. Each session has their own seperate delegate. The first session (downloadSession) is used to download large video files, the 2nd session (dataTaskSession) for a dataTask. The dataTask is auto-created only when a downloaded file completes.
I have managed to get this working successfully, however am not seeing what Id expect to see via logging.
1. I manually start a download of the video content, which goes and gets a small mpd file, and then it starts downlloading a single (large) video file and a smaller audio file. I put the app into background and watch the logs come in, I also proxy through Charles to see the files are all downloading.
2. As the files complete downloading, I get the 1st log, the call to 'handleEventsForBackgroundURLSession' with the identifier for the 1st session.
3. I then see 2 logs for the 1st session delegate's 'didCompleteWithError', that the downloads were successful.
4. I also see some logs indicating 2 dataTasks have been created, each for the audio and video files.
5. I then see the log for the 1st Session delegate's 'URLSessionDidFinishEventsForBackgroundURLSession'
Now heres where I do stuff that is only a guess cos I cant find any documentation on it.
I'll outline what I thought the docs are saying to do, however this solution ends up failing:
1. In the downloadSession delegate 'URLSessionDidFinishEventsForBackgroundURLSession', I assign the completionHandler from the AppDelegate handleEventsForBackgroundURLSession function
2. I nullify the AppDelegate's hold on the completionHandler
3. I call the completionHandler if the number of tasks for the downloadSession is 0
This resulted in the 2 dataTasks being logged ast starting, but not completing in the background. Only after I brought the app to foreground, the dataTaskSession's delegate didCompleteWithError function reports an error "Lost connection to background transfer service\134"
Also, in my wifi proxy app, the 2 dataTasks do not run.
My solution so far, is to check in the downloadSession delegate 'URLSessionDidFinishEventsForBackgroundURLSession' that the downloadSession tasks count as well as the dataTaskSession tasks count is 0 before calling the completionHandler. What I see logged is that the completionHandler is not called because the dataTaskSession tasks count is 2. This results in no errors that I can see and the dataTasks complete successfully.
But what I dont see is another call to the AppDelegate 'handleEventsForBackgroundURLSession' with the identifier for the dataTaskSession's identifier, after the dataTasks have completed. Nor do I see a call to the dataTaskSession's delegate's 'URLSessionDidFinishEventsForBackgroundURLSession'. And I donot see any further calls to the downloadSession delegate's 'URLSessionDidFinishEventsForBackgroundURLSession'.
Which then suggests the completionHandlers for each background session never gets called.
So I wanted to ask, is this a bad thing? Is there some sort of cleanup that needs to be done that Im not doing? Or maybe all my logging is just not making it to output? Am I right in thinking that 2 different sessions and their different delegates should have their completionHandlers called seperately and shouldnt affect the other or the nsurlsessiond?