diff --git a/SDWebImageSwiftUI/Classes/ImageManager.swift b/SDWebImageSwiftUI/Classes/ImageManager.swift index eb5ec273..14238e65 100644 --- a/SDWebImageSwiftUI/Classes/ImageManager.swift +++ b/SDWebImageSwiftUI/Classes/ImageManager.swift @@ -71,7 +71,7 @@ public final class ImageManager : ObservableObject { /// - Parameter url: The image url /// - Parameter options: The options to use when downloading the image. See `SDWebImageOptions` for the possible values. /// - Parameter context: A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold. - public func load(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) { + public func load(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) async { let manager: SDWebImageManager if let customManager = context?[.customManager] as? SDWebImageManager { manager = customManager @@ -127,7 +127,7 @@ public final class ImageManager : ObservableObject { } /// Cancel the current url loading - public func cancel() { + public func cancel() async { if let operation = currentOperation { operation.cancel() currentOperation = nil diff --git a/SDWebImageSwiftUI/Classes/WebImage.swift b/SDWebImageSwiftUI/Classes/WebImage.swift index 3dab6a3c..ba5ee741 100644 --- a/SDWebImageSwiftUI/Classes/WebImage.swift +++ b/SDWebImageSwiftUI/Classes/WebImage.swift @@ -168,17 +168,21 @@ public struct WebImage : View where Content: View { setupInitialState() // Load Logic .onAppear { - guard self.imageConfiguration.retryOnAppear else { return } - // When using prorgessive loading, the new partial image will cause onAppear. Filter this case - if self.imageManager.error != nil && !self.imageManager.isIncremental { - self.imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context) + Task { + guard self.imageConfiguration.retryOnAppear else { return } + // When using prorgessive loading, the new partial image will cause onAppear. Filter this case + if self.imageManager.error != nil && !self.imageManager.isIncremental { + await imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context) + } } } .onDisappear { guard self.imageConfiguration.cancelOnDisappear else { return } // When using prorgessive loading, the previous partial image will cause onDisappear. Filter this case if self.imageManager.error != nil && !self.imageManager.isIncremental { - self.imageManager.cancel() + Task { + await imageManager.cancel() + } } } } @@ -245,14 +249,16 @@ public struct WebImage : View where Content: View { self.imageManager.failureBlock = self.imageHandler.failureBlock self.imageManager.progressBlock = self.imageHandler.progressBlock if imageModel.url != imageManager.currentURL { - imageManager.cancel() - imageManager.image = nil - imageManager.imageData = nil - imageManager.cacheType = .none - imageManager.error = nil - imageManager.isIncremental = false - imageManager.indicatorStatus.isLoading = false - imageManager.indicatorStatus.progress = 0 + Task { + await imageManager.cancel() + imageManager.image = nil + imageManager.imageData = nil + imageManager.cacheType = .none + imageManager.error = nil + imageManager.isIncremental = false + imageManager.indicatorStatus.isLoading = false + imageManager.indicatorStatus.progress = 0 + } } } @@ -331,7 +337,9 @@ public struct WebImage : View where Content: View { self.setupManager() if (self.imageManager.error == nil) { // Load remote image when first appear - self.imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context) + Task { + await imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context) + } } return setupPlaceholder() }