Skip to content

Commit ca18735

Browse files
Fix for Hang Issue: Improving App Performance with Asynchronous Image Loading
1 parent 5d462f7 commit ca18735

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

SDWebImageSwiftUI/Classes/ImageManager.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public final class ImageManager : ObservableObject {
7171
/// - Parameter url: The image url
7272
/// - Parameter options: The options to use when downloading the image. See `SDWebImageOptions` for the possible values.
7373
/// - 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.
74-
public func load(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
74+
public func load(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) async {
7575
let manager: SDWebImageManager
7676
if let customManager = context?[.customManager] as? SDWebImageManager {
7777
manager = customManager
@@ -127,7 +127,7 @@ public final class ImageManager : ObservableObject {
127127
}
128128

129129
/// Cancel the current url loading
130-
public func cancel() {
130+
public func cancel() async {
131131
if let operation = currentOperation {
132132
operation.cancel()
133133
currentOperation = nil

SDWebImageSwiftUI/Classes/WebImage.swift

+22-14
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,21 @@ public struct WebImage<Content> : View where Content: View {
168168
setupInitialState()
169169
// Load Logic
170170
.onAppear {
171-
guard self.imageConfiguration.retryOnAppear else { return }
172-
// When using prorgessive loading, the new partial image will cause onAppear. Filter this case
173-
if self.imageManager.error != nil && !self.imageManager.isIncremental {
174-
self.imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context)
171+
Task {
172+
guard self.imageConfiguration.retryOnAppear else { return }
173+
// When using prorgessive loading, the new partial image will cause onAppear. Filter this case
174+
if self.imageManager.error != nil && !self.imageManager.isIncremental {
175+
await imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context)
176+
}
175177
}
176178
}
177179
.onDisappear {
178180
guard self.imageConfiguration.cancelOnDisappear else { return }
179181
// When using prorgessive loading, the previous partial image will cause onDisappear. Filter this case
180182
if self.imageManager.error != nil && !self.imageManager.isIncremental {
181-
self.imageManager.cancel()
183+
Task {
184+
await imageManager.cancel()
185+
}
182186
}
183187
}
184188
}
@@ -245,14 +249,16 @@ public struct WebImage<Content> : View where Content: View {
245249
self.imageManager.failureBlock = self.imageHandler.failureBlock
246250
self.imageManager.progressBlock = self.imageHandler.progressBlock
247251
if imageModel.url != imageManager.currentURL {
248-
imageManager.cancel()
249-
imageManager.image = nil
250-
imageManager.imageData = nil
251-
imageManager.cacheType = .none
252-
imageManager.error = nil
253-
imageManager.isIncremental = false
254-
imageManager.indicatorStatus.isLoading = false
255-
imageManager.indicatorStatus.progress = 0
252+
Task {
253+
await imageManager.cancel()
254+
imageManager.image = nil
255+
imageManager.imageData = nil
256+
imageManager.cacheType = .none
257+
imageManager.error = nil
258+
imageManager.isIncremental = false
259+
imageManager.indicatorStatus.isLoading = false
260+
imageManager.indicatorStatus.progress = 0
261+
}
256262
}
257263
}
258264

@@ -331,7 +337,9 @@ public struct WebImage<Content> : View where Content: View {
331337
self.setupManager()
332338
if (self.imageManager.error == nil) {
333339
// Load remote image when first appear
334-
self.imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context)
340+
Task {
341+
await imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context)
342+
}
335343
}
336344
return setupPlaceholder()
337345
}

0 commit comments

Comments
 (0)