Swift Concurrency Advanced Training Kata #9: Download the evidence
Our detectives picked up a mysterious tape covered in blood, gore, and fingerprints. After attempting to play it in their (ancient) tape player, they realised it was distorted beyond understanding.
Unfortunately, the tape deck’s mechanism wiped away all the incriminating DNA evidence, so we pretty much just have the audio to go on.
Fortunately, the lab was able to read the magnetic data and secure a digital copy. Therefore, your first task is to download the asset and play it back.
The tape download system is mostly in place: we kick off a task in ForensicLabViewModel.startDownload(), load up the metadata around the tape, and assemble the URL endpoint to locate the actual remote source file.
We also have a TapeDownloadManager that streams the tape data from the URL, writes it to a file, and returns the final file URL so we can play it.
We just need to finalise the performDownload() wiring in ForensicLabViewModel.
private func performDownload(
metadata: ForensicTapeMetadata,
downloadURL: URL
) async
Make sure to send back download progress to ensure a responsive UI during the long-running file download, as well as returning the final file URL.You will want to pass the byteSize property on ForensicTapeMetadata as the downloader’s expectedBytes.
Experience the full advanced Swift Concurrency training here: 🥋