SDWebImage / SDWebImageSwiftUI

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

Xcode 14 - SwiftUI warning #222

Closed guidev closed 1 year ago

guidev commented 2 years ago

If you try to run the sample app on Xcode 14 beta 5 you get the following warnings.

Screenshot 2022-08-09 at 18 38 52
guidev commented 2 years ago
Screenshot 2022-08-09 at 18 45 56
Alex-Coetzee commented 2 years ago

Getting this too :)

ivankodrnja commented 2 years ago

I'm getting identical warnings as well. Will you be able to fix them?

Teglgaard commented 2 years ago

+1 At the same time our logs are spammed with [SwiftUI] Publishing changes from within view updates is not allowed, this will cause undefined behavior.

lukeredpath commented 2 years ago

Also seeing this - this is a genuine bug within the library because it is triggering a method from within the view body that has side effects, one of which is mutating some published state:

Specifically, WebImage does this:

    public var body: some View {
        // 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()
        }
        ...snip...
    }

I have no context as to why this change was added but it is fundamentally wrong. Side effects should not be performed from within a SwiftUI's view body - this body method can potentially called many times (every time the view's state changes). In this case, calling .load() not only triggers a side effect, but it causes the @Published isLoading to be mutated in the middle of the view update cycle.

BugMonkey commented 1 year ago

+1

RMehdid commented 1 year ago

also having the same issue, with the bug affecting my navigationStack and moves me back to previous page, needs a fix asap, at least a workaround for now...

BugMonkey commented 1 year ago

I noticed that the picture flickered a bit in iOS16 beta 5 🥲

tichise commented 1 year ago

I encountered a similar message in my environment.

image
yoshirozay commented 1 year ago

+1

frlefebvre commented 1 year ago

Any chance this gets fixed before iOS 16's release?

dreampiggy commented 1 year ago

Hi.

This current repo's code, need a refactory to make it works with latest SwiftUI. And drop the usage of that ObservedObject using StateObject.

If someone has expert coding experience, PR is welcomed.

Currently I'm working on LLVM/Swift toolchain in this year, so may not have enough time to maintain this changes. But I'll try to back to this repo once I have some extra time.

Sri2611 commented 1 year ago

+1

garrettrayj commented 1 year ago

Following @lukeredpath's hint, I made some changes that seem to fix things on iOS 16.

https://github.com/SDWebImage/SDWebImageSwiftUI/compare/master...garrettrayj:ios16-undefined-behavior

I haven't tested AppKit and I bet backwards compatibility is messed up, but the changes appear mainly to be removing old workarounds that are no longer needed. Story of my life with SwiftUI, so I'm feeling confident enough to roll with 'em for the sake of getting iOS 16 updates out the door.

lukeredpath commented 1 year ago

If you don't need backwards compatibility another option would be to see if the built in AsyncImage does what you need and have one less third party dependency in your codebase. That's my plan.

Teglgaard commented 1 year ago

@garrettrayj - Great work - Will you make a PR? Then it can be reviewed.

tichise commented 1 year ago

I had the same error with iOS16 RC version.

Jeyhey commented 1 year ago

This warning appears with the latest SwiftUI update but the issue impacts all SwiftUI versions. For instance, I have observed that screens that have a SDWebImage as first element are not correctly rendered in a navigation animation (i.e. when they swipe in from right to left).

dreampiggy commented 1 year ago

Please have a try with v2.1.0

Jeyhey commented 1 year ago

Seems to work perfectly now. Thank you!!