AsyncImage - Cancelled Loading before View is Visible

I have been playing around with the new AsyncImage Api in SwiftUI

I am using the initialiser that passes in a closure with the AsyncImagePhase, to view why an image may not load, when I looked at the error that is passed in if the phase is failure, the localised description of the error is "Cancelled" but this is happening before the view is being displayed.

I am loading these images in a list, I imagine I am probably doing something which is causing the system to decide to cancel the loading, but I cannot see what.

Are there any tips to investigate this further?

"...but I cannot see what.", we cannot see it either if you don't show us some code. "Are there any tips to investigate this further?, yes show us the code you are using.

I have the same issue. List cells that show up when the app loads have images loaded fine, but as soon as I start scrolling any cells below the "fold" all have blank images. Checking the error code from the phase callback it shows the classic URL "cancelled" error (code 999 I believe?).

I suspect this may be a beta 1 SDK bug as it's hard to have a bug in the following, almost literally copied over from the Apple docs:

    AsyncImage(url: url) { phase in
        if let image = phase.image  {
            image
        } else if phase.error != nil {
            Image(systemName: "exclamationmark.triangle").padding()
            // the error here is "cancelled" on any view that wasn't visible at app launch
       } else {
            Spinner().padding()
        }
    }

For what it's worth, I did this hack in my code to get around this, hopefully temporarily. Not sure if I'd ship it :) but it keeps me moving forward while AsyncImage is either fixed, or I end up finding out there's some gotcha that I'm doing in my code that's causing it to cancel its loads in List cells. Time will tell :)

struct AsyncImageHack<Content> : View where Content : View {

    let url: URL?
    @ViewBuilder let content: (AsyncImagePhase) -> Content    

    @State private var currentUrl: URL?
    
    var body: some View {
        AsyncImage(url: currentUrl, content: content)
        .onAppear {
            if currentUrl == nil {
                DispatchQueue.main.async {
                    currentUrl = url
                }
            }
        }
    }
}

Confirming that this is still an issue in Xcode 13.0 beta 3.

From the iOS & iPadOS 15 beta 4 release notes:

Resolved in iOS & iPadOS 15 beta 4

AsyncImage in List no longer cancels image downloads prematurely. (78187465, 78727070)

I have the same issue with iOS 15.2. Actually the reason is clear and iOS behavior is acceptable. I am trying to fetch 20 images at the same time... On the other hand I have to overcome this issue... So my quick workaround is like the following;

I hope it works for your scenario. I also try to improve it(, if I have time).

AsyncImage(url: url) { phase in

	switch phase {
		case .success(let image):
			image
				.resizable()
				.scaledToFill()

		case .failure:

			//Call the AsynchImage 2nd time - when there is a failure. (I think you can also check NSURLErrorCancelled = -999)
			AsyncImage(url: url) { phase in
				if let image = phase.image {
					image
						.resizable()
						.scaledToFill()
				} else{
					Image(systemName: "xmark.octagon")
				}
			}

		//...

	}
}

Im still facing this issue with AsyncImage inside List in XCode Version 14.0 (14A309) for iOS 16.0

This is still a bug in iOS 16. The only solution that I found is to use ScrollView + LazyVStack instead of List.

I have the same problem too...even with the ScrollView and LazyVStack

This conversation in the Kingfisher async image library captures the ongoing issue(s) for many of us, including additional workarounds. There was a Feedback case filed (FB11564208), but those aren't public so I'm not sure how to report status.

There's another thread with a minimal working example here https://developer.apple.com/forums/thread/718480 Can someone from Apple take a look?

Can anyone confirm that the problem is still present on iOS17.2 / Xcode15.1?

Still an issue! Testing with ScrollView + LazyVStack. Xcode 15.2.0 / iOS Simulator 17.2

I think the root of this is a Problem with handling the Cache of the Images. Which is great implemented in a "one liner" now but the customer complain it does miss often Images in a List.

In the moment I put a cache killer on the Url it works great.

But if you don't have access to the Image Server Setup to ignore the cache killer your should check Traffic.

AsyncImage - Cancelled Loading before View is Visible
 
 
Q