dmytro-anokhin / url-image

AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crash in Async Operation

greese opened this issue · comments

I think there's a bug in here in ImageDecoder.swift:

public func createFrameImage(at index: Int, subsamplingLevel: SubsamplingLevel = .default, decodingOptions: DecodingOptions = .default) -> CGImage? {

...
case .asynchronous:
// Don't consider the subsamplingLevel when comparing the image native size with sizeForDrawing.
guard var size = frameSize(at: index) else {
return nil
}

            if let sizeForDrawing = decodingOptions.sizeForDrawing {
                // See which size is smaller: the image native size or the sizeForDrawing.
                if sizeForDrawing.width * sizeForDrawing.height < size.width * size.height {
                    size = sizeForDrawing
                }
            }

            options = imageSourceAsyncOptions(sizeForDrawing: size, subsamplingLevel: subsamplingLevel)
            **image = CGImageSourceCreateThumbnailAtIndex(imageSource, index, options)**

...

I am getting an occasional crash here and it appears to occur when there is reloading images from the cache. If I felt more confident in my Swift, I'd wrap that bolded line in a DispatchQueue.main.async { } block as I think what is happening is that it's trying to update the UI from outside the main queue. Maybe?

In particular, it seems this code is running inside the DiskCache decode queue.
Screen Shot 2021-04-22 at 19 39 27

As with any async bug based on reloading junk from cache, this is a pain to actually make happen. But the most common scenario is when I am navigating from one view to a previously viewed view with a lot of images in it.

Hey, thank you for reporting. This looks like thread explosion, what explains why you encounter it when there are many images. Which device/OS version you running?

tvOS, both in simulator and on device (Apple 4k for both). It's a free app in the App Store called Sparq Festival Platform if it helps to look at it.

It does load a lot of images. Basically, it's a streaming platform and we have rows of lazyhstacks forming a carousel of content on each page.

Hey, version 2.2.5 limits number of concurrent operations with disk cache. This should solve the problem.