Accessing the Cached Response
You created a URLRequest to set cachePolicy. You can also access the cached response, including its data and its URLResponse object.
Scroll back up to the TODO just below the AsyncImage(request:) call:
Display cached response data count
Replace this with the following code:
if let response = URLCache.shared.cachedResponse(for: request) {
Text("Response Data (KB): " + String(response.data.count / 1024))
}
You must unwrap the return value of cachedResponse(for:). It has an unavoidable race condition when attempting to read or write responses for the same request, and sometimes the value doesn’t exist.
Now, when you tap a list item, you might see the size of its cached response data:
This screenshot from my iMac shows response data size 158 KB for the last list item. On my laptop, the preview displays response data size 5166 KB.
The primaryImageSmall url in #Preview is the same as the last list item. When you download and save this url, the jpg file is 5.3 MB.
Using a Custom URLSession
Another feature of AsyncImage caching is specifying a custom URLSession for AsyncImage to use when downloading the image data. This is the last TODO, between the two PlaceholderView(note:) calls:
Set custom URLSession for AsyncImage
TheMetStore creates a custom URLSession, and ContentView passes it to ObjectView. All you need to do is replace the TODO with this modifier:
.asyncImageURLSession(session)
Also, to track the usage in this custom cache, add this code below Text("Shared Cache..."):
Text("Custom Cache (KB): " +
String((session.configuration.urlCache!.currentDiskUsage / 1024)))
Now, see what happens when you tap a list item:
Custom Cache 0 KB!? Go back to the list, turn off wifi, then tap the same list item … no image! AsyncImage might be caching the image in the shared cache, but it’s trying to retrieve it from the custom cache.
Is AsyncImage still caching in the shared cache? Turn wifi on and search for “cat” or something that returns a lot of objects, then tap several of them, keeping an eye on the Shared Cache size. After one or two objects, the size stops increasing — no caching is happening. Although the same thing happens when you comment out .asyncImageURLSession(session) and change cachePolicy to reloadIgnoringLocalCacheData — the shared cache size doesn’t increase after the first one or two downloads.
Well, this is beta 1. Hopefully, by the time you’re reading this, asyncImageURLSession will be working.
Note: In
AsyncImage and HTTP caching in iOS 27, Artem Mirzabekian applies the modifier to a subtree, to allow the view hierarchy to decide which session its images should use. For
ObjectView, you would move
asyncImageURLSession to modify the enclosing
VStack.
Running on a Simulator
Try running this app on an iOS 27 simulator. Everything works pretty much the same as in the preview canvas but, on a simulator, you can reset the cache by deleting the app from the home screen.
Where To Go From Here?
Download the final project using the Download Materials button at the top or bottom of this article.
In this article, you learned about the new caching feature of AsyncImage and how to customize a URLRequest or URLSession to manage the cache. Now you have a quick and easy way to download and cache images in your apps.