Skip to content

Commit a2bcffe

Browse files
committed
Fix the issue that WebImage's onSuccess does not get called when memory cache hit for new created View Struct
1 parent 4336985 commit a2bcffe

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

SDWebImageSwiftUI/Classes/ImageManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ImageManager : ObservableObject {
1919
weak var currentOperation: SDWebImageOperation? = nil
2020
var isSuccess: Bool = false // true means request for this URL is ended forever, load() do nothing
2121
var isIncremental: Bool = false // true means during incremental loading
22+
var isFirstLoad: Bool = true // false after first call `load()`
2223

2324
var url: URL?
2425
var options: SDWebImageOptions
@@ -39,6 +40,7 @@ class ImageManager : ObservableObject {
3940
}
4041

4142
func load() {
43+
isFirstLoad = false
4244
if currentOperation != nil {
4345
return
4446
}

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ public struct WebImage : View {
5858
}
5959
}
6060
self.imageManager = ImageManager(url: url, options: options, context: context)
61-
// load remote image here, SwiftUI sometimes will create a new View struct without calling `onAppear` (like enter EditMode) :)
62-
// this can ensure we load the image, SDWebImage take care of the duplicated query
63-
self.imageManager.load()
6461
}
6562

6663
public var body: some View {
67-
Group {
64+
// load remote image when first called `body`, SwiftUI sometimes will create a new View struct without calling `onAppear` (like enter EditMode) :)
65+
// this can ensure we load the image, and display image synchronously when memory cache hit to avoid flashing
66+
// called once per struct, SDWebImage take care of the duplicated query
67+
if imageManager.isFirstLoad {
68+
imageManager.load()
69+
}
70+
return Group {
6871
if imageManager.image != nil {
6972
if isAnimating && !self.imageManager.isIncremental {
7073
if currentFrame != nil {

0 commit comments

Comments
 (0)