Skip to content

Commit

Permalink
Fix the issue that WebImage's onSuccess does not get called when memo…
Browse files Browse the repository at this point in the history
…ry cache hit for new created View Struct
  • Loading branch information
dreampiggy committed Feb 1, 2020
1 parent 3353deb commit 74eb6b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions SDWebImageSwiftUI/Classes/ImageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,6 +40,7 @@ class ImageManager : ObservableObject {
}

func load() {
isFirstLoad = false
if currentOperation != nil {
return
}
Expand Down
11 changes: 7 additions & 4 deletions SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ public struct WebImage : View {
/// - Parameter context: A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
public init(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
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 animated {
if currentFrame != nil {
Expand Down

0 comments on commit 74eb6b5

Please sign in to comment.