URLSessionDownloadTask: Download Files Without Hogging Memory

URLSessionDownloadTask downloads files directly to disk, not memory, making it ideal for large assets. Use it for videos or updates, especially in the background. The footgun: you must move the file from its temporary location or the OS will delete it.
Why it exists
Apps often need to download large files without crashing due to memory pressure. Holding a 500MB video file in memory is a recipe for disaster. URLSessionDownloadTask solves this by offloading the data directly to the device's disk, using it as a buffer instead of RAM.
The mental model
Think of URLSessionDownloadTask as a managed file transfer service. Instead of getting raw data chunks in memory like with a URLSessionDataTask, you tell the system "download this URL to a file." The OS handles the entire process, even in the background, and then hands you a temporary file path, saying, "Here's your file, move it somewhere safe before I clean up."
How it works
You create a URLSessionDownloadTask with a URL. The system downloads the content to a temporary file in your app's sandbox, with progress reported via delegate methods. Upon completion, the urlSession(_:downloadTask:didFinishDownloadingTo:) delegate method is called, providing a URL to this temporary file. Your code must then use FileManager to move or copy this file to a permanent location (like the Documents directory) before the delegate method returns. If you fail to move it, the temporary file is deleted automatically. The task also supports pausing and resuming: call cancel(byProducingResumeData:) to get a data blob that you can use to create a new task and continue the download later.
When to use it
Use it for any network resource that is too large to comfortably fit in memory. This is the standard for downloading files like videos, audio archives, or software updates. It is essential for downloads that need to continue if the user backgrounds the app.
When not to use it
For small API responses like JSON or XML, a URLSessionDataTask is simpler and more efficient. Using a download task for a few kilobytes of data is overkill, as it involves unnecessary file I/O when keeping the data in memory is perfectly safe and straightforward.
One canonical example
A podcast app downloads a new episode. It creates a URLSessionDownloadTask for the MP3 file. The user can leave the app, and the download continues. When finished, the app's delegate moves the temporary MP3 file into a permanent "Episodes" folder, making it available for offline listening.
Interview question
After a URLSessionDownloadTask successfully completes, what crucial step must an app perform to retain the downloaded file?
- a.Rename the temporary file to its intended final filename.
- b.Move the temporary file from its given path to a permanent storage location.Correct
- c.Store the downloaded content in an in-memory data structure.
- d.Set a flag on the temporary file to prevent the operating system from deleting it.
Why? this is the answer
The URLSessionDownloadTask downloads to a temporary file that the OS will delete if not moved. Therefore, the app must move or copy the file to a permanent location. Simply renaming the temporary file (Option A) will not prevent its deletion by the operating system.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
- #ios
- #swift
- #networking
- #urlsession
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.
See open roles