Hi, I've got a background download being completed when the app isn't in memory. The app has been killed using exit
so the URLSession delegates are still able to be hit. I have logging in place confirming that acts as expected.
What doesn't work as expected, is when the method didFinishDownloadingTo
is called from the background on the URLSession delegate, and I attempt to move the file (a largish file ~400mb), the temp file isn't moved and is deleted once the method returns. When I user smaller file I have had successes but not every time. Moving the file produces no error in the try/catch. Any suggestions?
Code below:
func urlSession(_: URLSession, downloadTask task: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
Logging.error("Background - did finish downloading \(task.taskDescription)")
do {
let appSuportUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let locationUrl = appSuportUrl.appendingPathComponent(location.lastPathComponent)
Logging.error("Background - file \(locationUrl)")
try? FileManager.default.moveItem(atPath: location.path, toPath: locationUrl.path)
Logging.error("Background - wrote file \(locationUrl)") // This is excuted but it isn't correct as the file is never moved
} catch (let writeError) {
Logging.error("Background - File save failed: \(writeError.localizedDescription)") // This is never hit, indicating there is no error
}
}