Question:
What is the standard, most reliable way to manage temporary files associated with a URLSessionDownloadTask
that has been terminated abnormally due to a network error or other issues?
Details
Hello,
I'm currently developing a feature to download multiple files concurrently on iOS using URLSessionDownloadTask, and I have a question regarding the lifecycle of the temporary files created during this process.
As I understand it, URLSessionDownloadTask stores incoming data in a temporary file within the tmp directory, typically with a name like CFNetworkDownload_*.tmp
.
In my testing, temporary files are managed correctly in the normal scenario. For instance, when I call the cancel() method on an active downloadTask and then release all references to it, the corresponding temporary file is automatically cleaned up from the tmp directory shortly after.
However, the problem occurs when a download is interrupted abnormally due to external factors, such as a lost network connection. In this situation, the urlSession(_:task:didCompleteWithError:) delegate method is called, but the associated temporary file is not deleted and remains in the tmp directory.
I've observed a particularly interesting behavior related to this. Immediately after the error occurs, if I check my app's storage usage in the iOS Settings app, the data size appears to have decreased momentarily. However, the tmp file has not actually been deleted, and after a short while, the storage usage is recalculated to include the size of this orphaned temporary file.
Since my app does not support resuming interrupted downloads, these leftover files become orphaned and unnecessarily consume storage. Therefore, I want to ensure they are all reliably deleted.
With this context, I'd like to ask the community:
What is the standard, most reliable way to manage temporary files associated with a URLSessionDownloadTask that has been terminated abnormally due to a network error or other issues?
I am wondering if there is an official guide or a framework-level API to handle these orphaned files.
I would appreciate any advice from those with experience in this area. Thank you.
the urlSession(_:task:didCompleteWithError:) delegate method is called, but the associated temporary file is not deleted and remains in the tmp directory.
Right. The error passed to this call should include resume data, accessible via the NSURLSessionDownloadTaskResumeData
property. Standard practice is to create a new download task from that resume data. That task then takes ownership of the temporary file that holds the data downloaded so far. Eventually that’ll complete and the temporary file will become the real file passed to your did-finish-downloading delegate method.
Alternatively, you can call cancel()
on it and that’ll cancel it completely, cleaning up the temporary.
Since my app does not support resuming interrupted downloads
Hmmm, weird. Why not? Thats kinda what folks expect.
Still, if you absolutely don’t want to support resume, then I think your best option is to resume and then immediately cancel.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"