Thank you for making a very practical book.
I have a questions about the image cache mechanism introduced in Chapter 8.
The following is it.
[Question] Why doesn’t Download task run twice?
When the image cache does not exist, you are storing the Task in the cache as inProgress state. At the same time, you are copying the Task to download the image to the cache as an associated Value
let download: Task<UIImage, Error> = Task.detached {
guard let url = URL(string: "http://localhost:8080".appending(serverPath)) else {
throw "Could not create the download URL"
}
print("Download: \(url.absoluteString)")
let data = try await URLSession.shared.data(from: url).0
return try resize(data, to: CGSize(width: 200, height: 200))
}
cache[serverPath] = .inProgress(download)
And after that you await that Task.
do {
let result = try await download.value
add(result, forKey: serverPath)
return result
} catch {
....
}
When someone calls A at the same time, the following process will be executed because the cache exists.
if let cached = cache[serverPath] {
switch cached {
...
case .inProgress(let task):
return try await task.value
...
}
}
Since Task is a value type, I assumed that each await would request an image.
Why doesn’t Download task run twice?