Skip to content

Fix WebImage with indicator, when quick scroll without cache, may cause recursion #39

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

Merged
merged 2 commits into from
Nov 2, 2019
Merged
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
2 changes: 1 addition & 1 deletion Example/SDWebImageSwiftUIDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct ContentView: View {
"https://www.sample-videos.com/img/Sample-png-image-1mb.png",
"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
"http://via.placeholder.com/200x200.jpg"]
@State var animated: Bool = true // You can change between WebImage/AnimatedImage
@State var animated: Bool = false // You can change between WebImage/AnimatedImage

var body: some View {
#if os(iOS) || os(tvOS)
Expand Down
18 changes: 13 additions & 5 deletions SDWebImageSwiftUI/Classes/ImageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import SwiftUI
import SDWebImage

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

var manager = SDWebImageManager.shared
weak var currentOperation: SDWebImageOperation? = nil
var isFinished: Bool = false // true means request end, load() do nothing
var isIncremental: Bool = false // true means during incremental loading

var url: URL?
var options: SDWebImageOptions
Expand All @@ -35,7 +36,6 @@ class ImageManager : ObservableObject {
if currentOperation != nil {
return
}
self.image = nil
self.isLoading = true
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in
guard let self = self else {
Expand All @@ -55,6 +55,13 @@ class ImageManager : ObservableObject {
guard let self = self else {
return
}
if let error = error as? SDWebImageError, error.code == .cancelled {
// Ignore user cancelled
// There are race condition when quick scroll
// Indicator modifier disapper and trigger `WebImage.body`
// So previous View struct call `onDisappear` and cancel the currentOperation
return
}
if let image = image {
self.image = image
}
Expand All @@ -63,6 +70,7 @@ class ImageManager : ObservableObject {
self.isLoading = false
self.progress = 1
if let image = image {
self.isFinished = true
self.successBlock?(image, cacheType)
} else {
self.failureBlock?(error ?? NSError())
Expand Down
2 changes: 1 addition & 1 deletion SDWebImageSwiftUI/Classes/Indicator/Indicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct IndicatorViewModifier<T> : ViewModifier where T : View {
var indicator: Indicator<T>

func body(content: Content) -> some View {
if !imageManager.isLoading {
if imageManager.isFinished {
// Disable Indiactor
return AnyView(content)
} else {
Expand Down
2 changes: 1 addition & 1 deletion SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct WebImage : View {
}
let view = image
.onAppear {
if self.imageManager.image == nil {
if !self.imageManager.isFinished {
self.imageManager.load()
}
}
Expand Down