Git Product home page Git Product logo

Comments (3)

duyquang91 avatar duyquang91 commented on June 21, 2024 1

Yeah, you're right @hoc081098, that is how SwiftUI work. I understand that @huyparody get the sample code from README.md, it is my fault, it works perfectly in UIKit where the binding is invoked once, it would be a problem in SwiftUI as demonstrated. I'll update the README.md then.
Suggestion from @hoc081098 is perfect, I just wanna give @huyparody one more approach by using @Published

class ViewModel: ObservableObject {
...
@Published var errors: Errors?
init() {

   errorIndicator.errors
            .sink { error in self.errors = error }
            .store(in: &bag)
   }
...      
}      

Now you can use errors like other outputs such as isEnableLoginButton, it is more friendly with SwiftUI rather than Publisher
I'll close the issue, thank guys for the great discussion!

from activityindicator.

duyquang91 avatar duyquang91 commented on June 21, 2024

Hi @huyparody, thank for your report.
I can reproduce the issue from your sample project.
I just figure out if I sink the error publisher inside the ViewModel then the error publisher still works as expected.
I suppose the onReceive method of the View in the SwiftUI works unexpected, unfortunately I don't have much working experiences with SwifUI then I can't debug further. Hope you can help to figure it out from this point.

from activityindicator.

hoc081098 avatar hoc081098 commented on June 21, 2024

Every time ContentView.body is called, viewModel.errorPublisher will return a new Publisher instance (because it is a computed property).

That causes onReceive(_:perform:) to cancel the old Publisher and subscribe to the new one. The new one has the same behavior as ConcurrentValueSubject (aka. @Published private var relay: Error? ~ replay=1). So the latest error value is replayed the ContentView is rendered.

-> Avoid this by storing the Lazy Stored Properties that would persist between view re-renders.

class ViewModel: ObservableObject {
    lazy var errorPublisher: AnyPublisher<Error, Never> = errorIndicator.errors.eraseToAnyPublisher()
}

from activityindicator.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.