In WWDC 2021 video, Protect mutable state with Swift actors, they provide the following code snippet:
actor ImageDownloader {
private var cache: [URL: Image] = [:]
func image(from url: URL) async throws -> Image? {
if let cached = cache[url] {
return cached
}
let image = try await downloadImage(from: url)
cache[url] = cache[url, default: image]
return cache[url]
}
func downloadImage(from url: URL) async throws -> Image { ... }
}
The issue is that actors offer reentrancy, so cache[url, default: image] reference effectively ensures that even if you performed a duplicative request because of some race, that you at least check the actor’s cache after the continuation, ensuring that you get the same image for the duplicative request.
And in that video, they say:
A better solution would be to avoid redundant downloads entirely. We’ve put that solution in the code associated with this video.
But there is no code associated with that video. So, what is the better solution?
I understand the benefits of actor reentrancy (as discussed in SE-0306). E.g., if downloading four images, one does not want to prohibit reentrancy, losing concurrency of downloads. We would, effectively, like to await for the result of a duplicative prior request for a particular image if any, and otherwise just start a new downloadImage if not.
CodePudding user response:
After I came up with my original answer, I stumbled across Andy Ibanez’s write-up, Understanding Actors in the New Concurrency Model, in which he does not provide Apple’s code, but provides something inspired by it. The idea is very similar, but he uses an enum to keep track of the cached and pending responses:
actor ImageDownloader {
private enum ImageStatus {
case downloading(_ task: Task<UIImage, Error>)
case downloaded(_ image: UIImage)
}
private var cache: [URL: ImageStatus] = [:]
func image(from url: URL) async throws -> UIImage {
if let imageStatus = cache[url] {
switch imageStatus {
case .downloading(let task):
return try await task.value
case .downloaded(let image):
return image
}
}
let task = Task {
try await downloadImage(url: url)
}
cache[url] = .downloading(task)
do {
let image = try await task.value
cache[url] = .downloaded(image)
return image
} catch {
// If an error occurs, we will evict the URL from the cache
// and rethrow the original error.
cache.removeValue(forKey: url)
throw error
}
}
private func downloadImage(url: URL) async throws -> UIImage {
let imageRequest = URLRequest(url: url)
let (data, imageResponse) = try await URLSession.shared.data(for: imageRequest)
guard let image = UIImage(data: data), (imageResponse as? HTTPURLResponse)?.statusCode == 200 else {
throw ImageDownloadError.badImage
}
return image
}
}
CodePudding user response:
The key is to keep a reference to the Task, and if found, await its value.
Perhaps:
actor ImageDownloader {
private var cache: [URL: Image] = [:]
private var tasks: [URL: Task<Image, Error>] = [:]
func image(from url: URL) async throws -> Image {
if let image = try await tasks[url]?.value {
print("found request")
return image
}
if let cached = cache[url] {
print("found cached")
return cached
}
let task = Task {
try await download(from: url)
}
tasks[url] = task
defer { tasks[url] = nil }
let image = try await task.value
cache[url] = image
return image
}
private func download(from url: URL) async throws -> Image {
let (data, response) = try await URLSession.shared.data(from: url)
guard
let response = response as? HTTPURLResponse,
200 ..< 300 ~= response.statusCode,
let image = Image(data: data)
else {
throw URLError(.badServerResponse)
}
return image
}
}
