Fix the case that user should get the onSuccess callback even when memory cache hit

This commit is contained in:
DreamPiggy 2020-03-31 21:08:01 +08:00
parent 78d9bfbb60
commit aafa7a2222
2 changed files with 11 additions and 4 deletions

View File

@ -27,6 +27,7 @@ public final class ImageManager : ObservableObject {
var manager: SDWebImageManager
weak var currentOperation: SDWebImageOperation? = nil
var isFirstLoad: Bool = true // false after first call `load()`
var isFirstPrefetch: Bool = true // false after first call `prefetch()`
var url: URL?
var options: SDWebImageOptions
@ -108,6 +109,7 @@ public final class ImageManager : ObservableObject {
/// Prefetch the initial state of image, currently query the memory cache only
func prefetch() {
isFirstPrefetch = false
// Use the options processor if provided
let options = self.options
var context = self.context
@ -121,6 +123,9 @@ public final class ImageManager : ObservableObject {
if cacheType == .memory {
self.manager.imageCache.queryImage(forKey: key, options: options, context: context) { [unowned self] (image, data, cacheType) in
self.image = image
if let image = image {
self.successBlock?(image, cacheType)
}
}
}
}

View File

@ -57,13 +57,15 @@ public struct WebImage : View {
}
}
self.imageManager = ImageManager(url: url, options: options, context: context)
// this prefetch the memory cache of image, to immediately render it on screen
// this solve the case when `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :)
self.imageManager.prefetch()
}
public var body: some View {
Group {
// this prefetch the memory cache of image, to immediately render it on screen
// this solve the case when `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :)
if imageManager.isFirstPrefetch {
self.imageManager.prefetch()
}
return Group {
if imageManager.image != nil {
if isAnimating && !self.imageManager.isIncremental {
if currentFrame != nil {