In a SwiftUI recipe grid, AsyncImage thumbnails show their placeholders again and re-download when scrolled back into view; what does AsyncImage cache, and how do you fix it?
answer
- lazy rows are rebuilt
- shared session, nothing to configure
- transport-protocol caching from iOS 27
- init(request:) and a cache policy
- data cached, not decoded bitmaps
basics
~20 sBefore iOS 27, AsyncImage documents no cache and no download settings: it loads through the shared URLSession. From iOS 27 it caches image data following the transport protocol, init(request:) sets a cache policy and asyncImageURLSession(_:) supplies a session.
solid answer
~50 sA lazy grid recreates rows as they scroll back, and a recreated `AsyncImage` starts again at `.empty`, so the placeholder flashes and a request goes out unless a cache answers it. Up to iOS 26 the documentation says only that `AsyncImage` loads through the shared `URLSession`; it takes no request or session, so you cannot set a cache policy. In iOS 27 Apple documents that it caches downloaded image data following the transport protocol, in a cache created with a default `URLSessionConfiguration`; `init(request:scale:content:placeholder:)` accepts a `URLRequest` whose `cachePolicy` you set, and `asyncImageURLSession(_:)` gives a hierarchy its own session, and with it a `URLCache` you configure. The cache stores data, so images are still decoded again, and HTTP rules still apply: headers that forbid or shorten caching, or signed URLs that change on every response, defeat it. For iOS 17 to 26, gate the iOS 27 path with `#available` and use a small loader or an image pipeline library elsewhere.
code
swift · 59 linesimport SwiftUI
extension URLSession {
static let recipeImages: URLSession = {
let configuration = URLSessionConfiguration.default
configuration.urlCache = URLCache(memoryCapacity: 50_000_000, diskCapacity: 300_000_000)
return URLSession(configuration: configuration)
}()
}
struct RecipeThumbnail: View {
let url: URL
var body: some View {
Color.secondary.opacity(0.15)
.aspectRatio(1, contentMode: .fit)
.overlay { photo }
.clipped()
}
@ViewBuilder private var photo: some View {
if #available(iOS 27, *) {
AsyncImage(request: URLRequest(url: url, cachePolicy: .returnCacheDataElseLoad)) { image in
image.resizable().scaledToFill()
} placeholder: {
ProgressView()
}
} else {
AsyncImage(url: url) { image in
image.resizable().scaledToFill()
} placeholder: {
ProgressView()
}
}
}
}
struct RecipeGrid: View {
let photoURLs: [URL]
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 110))]) {
ForEach(photoURLs, id: \.self) { RecipeThumbnail(url: $0) }
}
}
.modifier(RecipeImageSession())
}
}
struct RecipeImageSession: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 27, *) {
content.asyncImageURLSession(.recipeImages)
} else {
content
}
}
}go deeper
Recall that lazy grids rebuild cells, so an AsyncImage can start loading again when it scrolls back.
Explain what changed in iOS 27: transport-protocol caching, init(request:) with a cache policy and asyncImageURLSession(_:).
Diagnose with the network instrument, check cache headers and URL stability, and plan the #available split for releases before iOS 27.
Weigh AsyncImage against a shared loader or pipeline library for the app's image-heavy screens, given the deployment target and memory budget.
## The symptom and its cause A recipe app shows hundreds of thumbnails in a `LazyVGrid`. Scrolling down and back up, the thumbnails flash their placeholders and the network inspector shows the same image URLs requested again. Two facts combine to produce this: - **Lazy containers rebuild rows.** `LazyVGrid` and `LazyVStack` create cells as they come on screen and do not promise to keep them alive once they leave. A cell that is built again gets a fresh `AsyncImage`, which starts again at the `.empty` phase. - **Whether the bytes come back from a cache is a separate question.** If nothing answers the request locally, the image downloads again. ## What AsyncImage documents, release by release | Release | How it loads | What you can configure | Documented caching | |---|---|---|---| | iOS 15 to iOS 26 | the shared `URLSession` | the URL and scale only | none described | | iOS 27 and later | the shared `URLSession`, or one you supply with `asyncImageURLSession(_:)` | a `URLRequest`, including `cachePolicy`, and a `URLSession` | image data cached following the transport protocol, in a cache created with a default `URLSessionConfiguration` | For iOS 27, Apple's `AsyncImage` page says it caches downloaded image data following the transport protocol, that the system creates the cache with a default `URLSessionConfiguration`, that you change the cache policy through a `URLRequest` passed to `init(request:scale:transaction:content:)`, and that `asyncImageURLSession(_:)` sets the `URLSession` that `AsyncImage` uses in a view hierarchy. The three `init(request:...)` initialisers and `asyncImageURLSession(_:)` are all marked available from iOS 27 in the SDK. Before that, the page promises no cache and there is no API to pass a request or a session. ## Fixing it on iOS 27 1. **Give the grid its own session.** Build a `URLSessionConfiguration.default`, set its `urlCache` to a `URLCache` with generous memory and disk capacity, and apply `.asyncImageURLSession(session)` to the grid. Apple's documentation says `AsyncImage` then performs its data tasks with that session, so its configuration is yours to set. 2. **Prefer cached data where staleness is fine.** Thumbnails rarely change, so a `URLRequest` with `cachePolicy` `.returnCacheDataElseLoad` uses a cached response when one exists, whatever its age. 3. **Check the server.** Following the transport protocol means HTTP caching rules apply: a `Cache-Control: no-store` header forbids storing the response, and a short max age expires entries quickly. 4. **Keep URLs stable.** A cache is keyed by request. Signed URLs whose query token changes on every API response never match a stored entry, so every scroll is a miss. ## What still is not cached - The documentation speaks of caching **image data**. A rebuilt cell still decodes the image again, and still shows `.empty` briefly while it does. - A 4,000-pixel original decoded for a 120-point thumbnail costs far more memory than the thumbnail needs. Downsampling and memory budgets are image-pipeline design, not something `AsyncImage` offers. - There is no prefetching, priority or cancellation control. ## Supporting iOS 17 to iOS 26 Most apps still deploy to earlier releases, so the iOS 27 APIs sit behind `if #available(iOS 27, *)`. For the older path you have three honest options: - accept the reloads for low-traffic screens where a single hero photo is all that loads; - write a small loader: a `URLSession` with a configured `URLCache`, an in-memory cache of decoded images keyed by URL, and a view that loads in `task(id: url)`; - adopt a third-party image pipeline library that adds memory and disk caches, decoding off the main thread, prefetching and downsampling. ## Fixes that do not work - Replacing `LazyVGrid` with a plain `VStack` inside a `ScrollView` keeps every cell alive, but it builds every cell up front, so every download starts at once, trading the flicker for memory and bandwidth. - Adding `.id(url)` to each `AsyncImage` does not create a cache; identity decides when SwiftUI keeps or replaces a view, not where bytes come from. - The `transaction` parameter only animates phase changes; it has nothing to do with caching. ## How to diagnose it Watch requests in the network instrument or a debugging proxy while you scroll. Repeated requests for the same URL with full responses mean there is no cache hit; check the response headers and whether the URL changes between requests. Re-test on an iOS 27 device with the request-based initialiser to confirm the cache is used.
- Why can AsyncImage still flash its placeholder on iOS 27 even when the data comes from the cache?A rebuilt cell is a new view that starts at `.empty` and must read and decode the image data again before reaching `.success`. The cache removes the network trip, not the decode or the phase change. Keeping the placeholder the same size and colour as the photo makes the brief flash far less visible.
- When is AsyncImage the right tool, and when should a screen use a dedicated image pipeline?AsyncImage suits a few images per screen, such as a profile or hero photo, and on iOS 27 a cached grid with stable URLs. Large scrolling grids that need downsampling, prefetching, priorities, memory budgets or support for releases before iOS 27 are better served by a shared loader or an image pipeline library.
- AsyncImage has always kept loaded images in an in-memory cache.
- Adding .id(url) to each AsyncImage makes it cache its image.
- Before iOS 27, a URLRequest cache policy can be passed to AsyncImage.
- Switching the lazy grid to a VStack is a free fix for the reloads.
- Caching image data means the image is never decoded again.
Assumes the iOS 27 SDK: AsyncImage's documented transport-protocol caching, init(request:...) and asyncImageURLSession(_:) all arrive in iOS 27; earlier releases offer none of them.