Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download() called too soon #21

Open
ghost opened this issue May 4, 2022 · 1 comment
Open

download() called too soon #21

ghost opened this issue May 4, 2022 · 1 comment

Comments

@ghost
Copy link

ghost commented May 4, 2022

Hello.

I'm using your package in a project and I noticed that Xcode gives the warning:

"Accessing StateObject's object without being installed on a View. This will create a new instance each time."

at the point where self.viewModel.download() is called, inside a view initialiser.

bug

That's happening because the download is being initiated when the view is initialised rather than when it's added to the view hierarchy. I think you should consider fixing your implementation by moving all the self.viewModel.download() calls out of the initialisers they're in and into a closure passed to an onAppear() view modifier, like so:

init(viewModel: ViewModel,
     @ViewBuilder content: @escaping (AsyncImagePhase) -> Content) {
    self._viewModel = .init(wrappedValue: viewModel)
    self.content = content
    // self.viewModel.download() // <--- remove this line
}

var body: some View {
    content(viewModel.phase)
        .onAppear { viewModel.download() } // <--- add this line
}

In any case, thanks for writing this package.

@yutailang0119
Copy link
Owner

Hi,

Thanks for using this library!

I would like to identify the details of this problem.
Please tell me in detail the version of SBPASyncImage and OS Runtime.

I guess here.

self.provider.task(url: url, scale: scale, transaction: transaction)

This problem is complicated.
If I adopt your suggestion, #16 will be reopened.

It can be solved by combining onChange(of:perform:), but this does not meet all the availability of supported OS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant