Daily iOS
Concurrency

Dictionary cache thread safety

In a message list screen, each cell downloads its profile image asynchronously and caches it in a [String: UIImage] dictionary. With several threads reading and writing at once, it crashes now and then. Why does it crash?

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

A Swift Dictionary isn't thread safe. If another thread reads while a write is rehashing or swapping the COW buffer, it touches a freed or half-moved buffer and crashes with a bad memory access.

Share a question