SDWebImage / SDWebImageSwiftUI

SwiftUI Image loading and Animation framework powered by SDWebImage
https://sdwebimage.github.io/SDWebImageSwiftUI
MIT License
2.18k stars 224 forks source link

Images are not updating properly in some cases #105

Closed alladinian closed 4 years ago

alladinian commented 4 years ago

So, I'm not sure if I'm abusing the API or it is actually a bug, but here is the problematic setup:

  1. A view that takes some id
  2. The initializer of the view instantiates an observable object to act as the "item details" fetcher
  3. The body of the view is recalculated when the fetcher is done

now, the WebImage takes a url, which of course is invalid (empty) while the fetcher is working...

Something like: WebImage(url: URL(string: observable.imageURL ?? ""))

which results in an empty image, even after the second pass of the body passes a valid image URL (verified in the debugger).

If instead of an empty URL I pass a static image URL, then everything works as expected:

WebImage(url: URL(string: observable.imageURL ?? "https://some_image_url"))

any ideas?

dreampiggy commented 4 years ago

Seems because of the nil URL does not trigger the ImageManager.$image observed object callback. So the internal SwiftUI.Image does not get updated.

I can update to have a check for this case, and always update ImageManger.$image even on error when loading.

dreampiggy commented 4 years ago

@alladinian A more easy to debug and re-producable demo is welcomed. You just need to simplify the core code usage, don't need to put your real project.

alladinian commented 4 years ago

That was a prompt reply, thanks!

Ok, this is a minimal example to reproduce the issue:

import SwiftUI
import SDWebImageSwiftUI

struct TestView: View {

    // Non-working version
    @State var urlString: String = ""

    // Working version
    @State var urlString: String = "https://via.placeholder.com/150"

    var body: some View {
        VStack {
            WebImage(url: URL(string: self.urlString))
                .frame(width: 300, height: 300, alignment: .center)
            Button(action: {
                self.urlString = "https://via.placeholder.com/300"
            }) {
                Text("Button")
            }
        }
    }
}

struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
    }
}

I guess I could help with fixing the bug but I need to familiarize myself with the codebase first, so please let me know if there is anything I can do to help.

dreampiggy commented 4 years ago

I'll have a check for this today and find a workaround. Please have a wait. (busy on daily job yesterday)

dreampiggy commented 4 years ago

Decide to revert changes about performance introduced in #92.

Same root case in #103

dreampiggy commented 4 years ago

@alladinian Please have a try again with v1.3.4

alladinian commented 4 years ago

@dreampiggy Indeed, the issue seems to be resolved in v1.3.4 👍