Daily iOS
Concurrency

Actor reentrancy

This image cache actor built with Swift Concurrency sometimes sends two network requests for the same key. The actor protects its state, so why does this happen?

actor ImageCache {
    private var cache: [URL: UIImage] = [:]
    func image(for url: URL) async throws -> UIImage {
        if let hit = cache[url] { return hit }
        let img = try await download(url)
        cache[url] = img
        return img
    }
}

Submit