Skip to content

Commit 4b8d5cd

Browse files
committed
Fix the issue when quick scroll, WebImage indicator will cause huge CPU usage because of recusive call. Ignore the onDisappear use cancel code
1 parent 46c841a commit 4b8d5cd

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

Example/SDWebImageSwiftUIDemo/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct ContentView: View {
3232
"https://www.sample-videos.com/img/Sample-png-image-1mb.png",
3333
"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
3434
"http://via.placeholder.com/200x200.jpg"]
35-
@State var animated: Bool = true // You can change between WebImage/AnimatedImage
35+
@State var animated: Bool = false // You can change between WebImage/AnimatedImage
3636

3737
var body: some View {
3838
#if os(iOS) || os(tvOS)

SDWebImageSwiftUI/Classes/ImageManager.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import SDWebImage
1212
class ImageManager : ObservableObject {
1313
@Published var image: PlatformImage?
1414
@Published var isLoading: Bool = false
15-
@Published var isIncremental: Bool = false
1615
@Published var progress: CGFloat = 0
1716

1817
var manager = SDWebImageManager.shared
1918
weak var currentOperation: SDWebImageOperation? = nil
19+
var isFinished: Bool = false
20+
var isIncremental: Bool = false
2021

2122
var url: URL?
2223
var options: SDWebImageOptions
@@ -35,7 +36,6 @@ class ImageManager : ObservableObject {
3536
if currentOperation != nil {
3637
return
3738
}
38-
self.image = nil
3939
self.isLoading = true
4040
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in
4141
guard let self = self else {
@@ -55,6 +55,13 @@ class ImageManager : ObservableObject {
5555
guard let self = self else {
5656
return
5757
}
58+
if let error = error as? SDWebImageError, error.code == .cancelled {
59+
// Ignore user cancelled
60+
// There are race condition when quick scroll
61+
// Indicator modifier disapper and trigger `WebImage.body`
62+
// So previous View struct call `onDisappear` and cancel the currentOperation
63+
return
64+
}
5865
if let image = image {
5966
self.image = image
6067
}
@@ -63,6 +70,7 @@ class ImageManager : ObservableObject {
6370
self.isLoading = false
6471
self.progress = 1
6572
if let image = image {
73+
self.isFinished = true
6674
self.successBlock?(image, cacheType)
6775
} else {
6876
self.failureBlock?(error ?? NSError())

SDWebImageSwiftUI/Classes/Indicator/Indicator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct IndicatorViewModifier<T> : ViewModifier where T : View {
3232
var indicator: Indicator<T>
3333

3434
func body(content: Content) -> some View {
35-
if !imageManager.isLoading {
35+
if imageManager.isFinished {
3636
// Disable Indiactor
3737
return AnyView(content)
3838
} else {

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public struct WebImage : View {
5252
}
5353
let view = image
5454
.onAppear {
55-
if self.imageManager.image == nil {
55+
if !self.imageManager.isFinished {
5656
self.imageManager.load()
5757
}
5858
}

0 commit comments

Comments
 (0)