Revert the changes to prefetch the image url from memory cache, because there are cases that the `onAppear` does not get called at all, which need at least update the remote URL. See #105

This commit is contained in:
DreamPiggy 2020-04-30 15:19:47 +08:00
parent 8e61e4451e
commit b927adbb79
2 changed files with 4 additions and 28 deletions

View File

@ -27,7 +27,6 @@ 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
@ -107,28 +106,6 @@ public final class ImageManager : ObservableObject {
}
}
/// Prefetch the initial state of image, currently query the memory cache only
func prefetch() {
isFirstPrefetch = false
var options = self.options
if options.contains(.fromLoaderOnly) {
// If user indeed ignore cache, don't do prefetch
return
}
// Use `.fromCacheOnly` to query cache only
options.insert(.fromCacheOnly)
var context = self.context ?? [:]
context[.queryCacheType] = SDImageCacheType.memory.rawValue
// Use `.queryCacheType` to query memory cache only
manager.loadImage(with: url, options: options, context: context, progress: nil) { (image, data, error, cacheType, finished, imageUrl) in
// This will callback immediately
self.image = image
if let image = image {
self.successBlock?(image, cacheType)
}
}
}
}
// Completion Handler

View File

@ -60,10 +60,9 @@ public struct WebImage : View {
}
public var body: some View {
// 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 {
imageManager.prefetch()
// This solve the case when WebImage created with new URL, but `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :)
if imageManager.isFirstLoad {
imageManager.load()
}
return Group {
if imageManager.image != nil {
@ -100,7 +99,7 @@ public struct WebImage : View {
setupPlaceholder()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.onAppear {
// load remote image when first appear
// Load remote image when first appear
if self.imageManager.isFirstLoad {
self.imageManager.load()
return