Fix the issue that WebImage's onSuccess does not get called when memory cache hit for new created View Struct

This commit is contained in:
DreamPiggy 2020-02-01 17:52:57 +08:00
parent 433698519a
commit a2bcffe29f
2 changed files with 9 additions and 4 deletions

View File

@ -19,6 +19,7 @@ class ImageManager : ObservableObject {
weak var currentOperation: SDWebImageOperation? = nil
var isSuccess: Bool = false // true means request for this URL is ended forever, load() do nothing
var isIncremental: Bool = false // true means during incremental loading
var isFirstLoad: Bool = true // false after first call `load()`
var url: URL?
var options: SDWebImageOptions
@ -39,6 +40,7 @@ class ImageManager : ObservableObject {
}
func load() {
isFirstLoad = false
if currentOperation != nil {
return
}

View File

@ -58,13 +58,16 @@ public struct WebImage : View {
}
}
self.imageManager = ImageManager(url: url, options: options, context: context)
// load remote image here, SwiftUI sometimes will create a new View struct without calling `onAppear` (like enter EditMode) :)
// this can ensure we load the image, SDWebImage take care of the duplicated query
self.imageManager.load()
}
public var body: some View {
Group {
// load remote image when first called `body`, SwiftUI sometimes will create a new View struct without calling `onAppear` (like enter EditMode) :)
// this can ensure we load the image, and display image synchronously when memory cache hit to avoid flashing
// called once per struct, SDWebImage take care of the duplicated query
if imageManager.isFirstLoad {
imageManager.load()
}
return Group {
if imageManager.image != nil {
if isAnimating && !self.imageManager.isIncremental {
if currentFrame != nil {