Daily iOS
Concurrency

Merging same-URL downloads

In a photo grid, several cells request the same photo URL at the same time, but the network download must happen only once. How would you keep in-flight Tasks inside an actor to merge the requests?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Keep a [URL: Task<UIImage, Error>] dictionary keyed by URL inside the actor; when a request comes in, if a Task for that URL already exists, await its value, and if not, create a new Task, put it in the dictionary first, then await it. The second cell doesn't start a download and shares the first Task's result.

Share a question