In a children's educational-video app where parents download whole episodes before a flight, how should the download queue work so episodes finish even if the app is closed?
answer
- a promise the parent is relying on
- a saved queue, not an in-memory list
- quality chosen for storage, up front
- resume from what is already on disk
- reconcile the queue on every launch
basics
~20 sPersist a queue entry per episode with its chosen storage quality, progress and state; hand transfers to the OS's background-transfer service; resume from bytes on disk; and reconcile on every launch, since a user force-quit can cancel transfers.
solid answer
~50 sA download is a promise to a parent, so its state must outlive the process. I'd persist one queue entry per episode: its id, the quality the parent picked for storage with the size shown up front, the pieces or bytes done, its state and a licence reference. At most one or two downloads run at a time, Wi-Fi-only if the parent says so. Transfers go to the OS's background-transfer service so they continue while the app is suspended, but on one platform a user force-quit cancels them and the app is not relaunched for them, so every launch reconciles the queue with what is on disk and resumes. A partial file resumes with a `Range` request guarded by `If-Range`, so a changed file restarts cleanly. Finished episodes live in persistent storage, never the purgeable cache, and only on-demand episodes offer a download at all.
code
json · 13 lines{
"episodeId": "ep-0412",
"title": "Why the Moon Changes Shape",
"storageQuality": "standard",
"expectedBytes": 412000000,
"state": "downloading",
"stopReason": null,
"piecesDone": 118,
"piecesTotal": 240,
"requiresWifi": true,
"licenceId": null,
"queuedAt": "2026-10-07T19:40:00Z"
}go deeper
Recall that an offline download needs a saved queue, a resume from what is already on disk, and a home outside the cache folder the OS may clear.
Explain the queue entry's fields, the states a parent sees, the range request that resumes a partial file, and why only on-demand episodes offer a download.
Walk the lifecycle edges: suspension, a system termination and background relaunch, a user force-quit that cancels transfers on one platform, and the launch-time reconcile that makes each safe.
Frame downloads as a product-wide policy: which titles and qualities may be downloaded, concurrency and network rules, and how much of a family's phone the app may claim.
## What makes a download different from a preload A **preload** is the app's own guess and can be thrown away. An **offline download** is a parent's explicit request, made in the evening before a 6 a.m. flight, and it is judged by one moment: the plane is in the air and the episode either plays or it does not. Everything in the design follows from that: the queue survives the process, progress is never lost, the result is never evicted by the app's caches, and the parent can see what is ready. Scenario assumptions, stated so the numbers can be checked: an episode is 20 to 25 minutes, roughly 300 to 450 MB at the app's standard quality and about twice that at high, and parents often queue five to ten episodes at once. ## The queue entry Each episode gets one **persisted record**, written before the first byte is requested and updated as work completes: - the **episode id** and title, so the list renders without the network; - the **storage quality** the parent chose, a fixed choice made once, before downloading, with its expected size; - **progress**: which pieces of the episode are complete, or the byte count of a single-file episode; - **state**, from the table below, and a **stop reason** if the parent paused it; - **constraints**, such as Wi-Fi only; - a **licence reference**, filled when the offline licence is acquired. The queue runs at most one or two downloads at once, so the first episode finishes early rather than ten finishing together at the last minute. ## States the parent sees | State | Meaning | What the screen shows | |---|---|---| | Queued | Waiting for its turn or a constraint | "Waiting for Wi-Fi" or "Next up" | | Downloading | Bytes arriving | Honest progress in percent | | Paused | The parent stopped it | Progress kept, a resume button | | Ready | Complete and licensed | "Ready to watch offline" | | Failed | Retries exhausted, or no space | The reason and a retry button | ## Surviving suspension, termination and force-quit 1. **Hand transfers to the OS.** The app's process is suspended soon after the parent locks the phone. A transfer handed to the OS's **background-transfer service** continues while the app is suspended; on one platform that service even survives the system ending the app. The other platform offers a job type for large transfers the user started. How long and how often such work may run is the background-budget question's. 2. **Reattach after a system termination.** On one platform, when the system ends a suspended app, it relaunches it in the background when a transfer needs attention, and the app must recreate its transfer session under the same identifier to collect results. 3. **Expect a force-quit to cancel.** On that same platform, a user swiping the app away cancels its background transfers, and the system does not relaunch the app for them. Do not assume either platform keeps a transfer the user killed. 4. **Reconcile on every launch.** Compare each queue entry with the files on disk and the transfers the OS reports, then resume whatever is incomplete. This one step makes all three cases above safe. ## Resuming a half-finished episode A long episode is fetched in many pieces; the queue records which are complete, so a resume skips them. A partially written single file resumes with an HTTP **`Range`** request from the byte after the last one stored, sent with **`If-Range`** carrying the file's strong entity tag. If the file on the server is unchanged, the reply is `206 Partial Content` with only the missing bytes; if it changed, the server sends the whole new file with `200`, so stale and new bytes are never spliced together. After the last piece, the app verifies the result before marking it **Ready**. ## Where finished episodes live, and who removes them - **Persistent app storage**, never the cache folder: both platforms may delete cache files when storage runs low, and a parent's download must not vanish that way. - **Excluded from device backup** on a platform that backs up app data: episodes are large and can be downloaded again. - **Removed only by a person or a rule the parent can see**: the parent deletes one, or the account signs out. A lapsed licence alone does not delete the file; it marks the episode as needing renewal. Uninstalling the app removes its storage too. - **Counted honestly**: the downloads screen shows how much space episodes use. Checking free space before a large batch is the local-store question's; licence windows and download caps are the licence question's. ## Only on-demand episodes A live stream has no end to save, and one platform's download API refuses a live stream outright. The catalogue offers the download button only on **on-demand** episodes.
- The parent picks high quality for ten episodes; what should the app show before starting?The total size of that choice next to the free space the app can see, and what the lower quality would take, from the catalogue's own per-episode sizes. Quality here is a storage choice made once, before downloading; the copy on disk holds that one quality, so offline playback must use only what was downloaded rather than reaching for the network.
- Why not keep the queue in memory and rebuild it from the episode list on launch?The process dies routinely while downloads run: suspended, ended by the system, or swiped away. An in-memory queue loses the chosen quality, the progress of each episode and which ones the parent cancelled, so the app either restarts from zero or resurrects downloads the parent removed. A persisted entry, updated as pieces finish, turns every launch into a cheap reconcile.
- A transfer handed to the OS keeps running even after the user force-quits the app.
- A live stream can be saved for offline viewing like any on-demand episode.
- Downloaded episodes belong in the cache folder, since they can be fetched again.
- Resuming a half-finished download means starting the episode again from byte zero.
- The download queue can live in memory, because the app is open while it downloads.