Daily iOS
Concurrency

Shared cache data race

Several image requests read and write the same in-memory cache dictionary at the same time. It crashes now and then, and the logs show the same URL downloaded twice. If you fixed it with an actor instead of a lock, which state would you put inside the actor?

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

Put both mutable pieces of state inside the actor, the dictionary of finished images and the list of requests currently downloading, and allow access from outside only through await; then the data race from concurrent writes to the dictionary disappears at compile time.

Share a question