Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Hang Issue: Improving App Performance with Asynchronous Image… #330

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions SDWebImageSwiftUI/Classes/ImageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 22 additions & 14 deletions SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,21 @@ public struct WebImage<Content> : 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()
}
}
}
}
Expand Down Expand Up @@ -245,14 +249,16 @@ public struct WebImage<Content> : 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
}
}
}

Expand Down Expand Up @@ -331,7 +337,9 @@ public struct WebImage<Content> : 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()
}
Expand Down
Loading