Skip to content

Commit d1acfc5

Browse files
authored
Merge pull request #39 from SDWebImage/fix_quick_scroll_webimage_cpu_usage
Fix WebImage with indicator, when quick scroll without cache, may cause recursion
2 parents 46c841a + df60030 commit d1acfc5

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
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: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import SwiftUI
1010
import SDWebImage
1111

1212
class ImageManager : ObservableObject {
13-
@Published var image: PlatformImage?
14-
@Published var isLoading: Bool = false
15-
@Published var isIncremental: Bool = false
16-
@Published var progress: CGFloat = 0
13+
@Published var image: PlatformImage? // loaded image, note when progressive loading, this will published multiple times with different partial image
14+
@Published var isLoading: Bool = false // whether network is loading or cache is querying
15+
@Published var progress: CGFloat = 0 // network progress
1716

1817
var manager = SDWebImageManager.shared
1918
weak var currentOperation: SDWebImageOperation? = nil
19+
var isFinished: Bool = false // true means request end, load() do nothing
20+
var isIncremental: Bool = false // true means during incremental loading
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)