Skip to content

Commit aafa7a2

Browse files
committed
Fix the case that user should get the onSuccess callback even when memory cache hit
1 parent 78d9bfb commit aafa7a2

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

SDWebImageSwiftUI/Classes/ImageManager.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public final class ImageManager : ObservableObject {
2727
var manager: SDWebImageManager
2828
weak var currentOperation: SDWebImageOperation? = nil
2929
var isFirstLoad: Bool = true // false after first call `load()`
30+
var isFirstPrefetch: Bool = true // false after first call `prefetch()`
3031

3132
var url: URL?
3233
var options: SDWebImageOptions
@@ -108,6 +109,7 @@ public final class ImageManager : ObservableObject {
108109

109110
/// Prefetch the initial state of image, currently query the memory cache only
110111
func prefetch() {
112+
isFirstPrefetch = false
111113
// Use the options processor if provided
112114
let options = self.options
113115
var context = self.context
@@ -121,6 +123,9 @@ public final class ImageManager : ObservableObject {
121123
if cacheType == .memory {
122124
self.manager.imageCache.queryImage(forKey: key, options: options, context: context) { [unowned self] (image, data, cacheType) in
123125
self.image = image
126+
if let image = image {
127+
self.successBlock?(image, cacheType)
128+
}
124129
}
125130
}
126131
}

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ public struct WebImage : View {
5757
}
5858
}
5959
self.imageManager = ImageManager(url: url, options: options, context: context)
60-
// this prefetch the memory cache of image, to immediately render it on screen
61-
// this solve the case when `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :)
62-
self.imageManager.prefetch()
6360
}
6461

6562
public var body: some View {
66-
Group {
63+
// this prefetch the memory cache of image, to immediately render it on screen
64+
// this solve the case when `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :)
65+
if imageManager.isFirstPrefetch {
66+
self.imageManager.prefetch()
67+
}
68+
return Group {
6769
if imageManager.image != nil {
6870
if isAnimating && !self.imageManager.isIncremental {
6971
if currentFrame != nil {

0 commit comments

Comments
 (0)