Thank you @DTS Engineer for the energy you put into helping us write better code, I really appreciate your commitment to this.
I had a couple of mostly unrelated points/responses
I want to give a bit of background to describe my original goals/intentions (just in case there is a better option I have not considered)
Something interesting my 'research' uncovered
Background
I've created an app for fetching and listening to podcasts. It includes a scan function that:
scans subscribed podcasts to see if there are known episodes it should download
makes api calls to fetch podcast metadata to see if there are any 'new' episodes
fetch these new episodes if we aren't currently storing too much new content (eg just because we have meta data for 10 episodes that are 2 hours each, don't download them all right away)
The code that does this work has multiple URLSession callbacks, state information and multiple error cases that need to be handled. The flow is a bit complex.
I thought this functionality would be better implemented using a single code path with async/await. At this point, based on what I've learned here it feels like async/await won't meet my needs because I want them to continue if the app moves to the background. (if a scan starts while the app is in the foreground, I want the user to be able to move the app to the background and have the scan continue; and also continue playing audio; as you pointed out, two complete independent flavours of background task)
So at this point, absent a third option I'm not yet aware of, I'm going to stick with my more complex completionHandler-based implementation of this scan function.
Something. I discovered
Out of curiosity, I modified my option1 implementation to use URLSession.shared instead of background-capable instance and..... option1 still works. I think this means my audio dropouts are not related to whether the URLSession is background able or not. If I had to guess, the only difference I see between the two options is the steps to put the data blob in a file.
In option1, URLSession creates the file in a temporary location and my code merely moves that file to the required location. In option2, my code actually creates the file. My current assumption is that there is something in the way my code is writing the file that is causing the problem.
But at this point, even if I can uncover the cause of the audio drop outs, I'll still be stuck as I don't see a way to use the data task, as they only work while the app is in the foreground.