I've just implemented background session downloads, and in testing (with 1044 downloadTasks), I'm seeing some strange behavior that's not 100% reproducible. Sometimes when I background the app, when I foreground it (or the OS does), the URLSessionDownloadDelegate's function
urlSession(_:downloadTask:didFinishDownloadingTo:)
gets called twice. I'm also logging the URLSessionTaskDelegate's function
urlSession(_:task:didCompleteWithError:)
and in this case, it does not get called between calls to didFinishDownloadingTo.
Both cases are being called with the exactly same task, session and location. The first call copies the location to a semi-permanent destination (and I confirmed that file is correct), and the second call fails on move because the destination already exists.
I can obviously work around this fairly easily, but wondering if I'm missing something or if there's a bug.
It does appear to happen more reliably when I background for 15 seconds or longer.
A second issue which is reproducible is that while backgrounded, some files are completing downloads and never calling the download delegate's
urlSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)
I tried resuming one or all of the tasks in applicationDidBecomeActive as suggested in multiple other forums posts, but neither of those seems to resolve the issue. Again, I can work around this (using a combination of totalBytesWritten and the known size of files which have completed downloads), but I'm wondering if I'm missing something obvious.
I actually thought that perhaps the resume() workaround was causing the first issue, but removing it does not have an effect.