I have a view model, that has a network service that returns us a Combine publisher. The VM reacts to this publisher by updating its state. And the UIViewController
would then update based on the VM state change. This was the setup for UIKit
.
Now I am introducing SwiftUI
views. I am new to swift UI and not sure how to use the VM's state publisher in SwiftUI.
The VM's state is a CurrentValueSubject
right now.
Thanks in advance.
I have a view model, that has a network service that returns us a Combine publisher. The VM reacts to this publisher by updating its state. And the UIViewController
would then update based on the VM state change. This was the setup for UIKit
.
Now I am introducing SwiftUI
views. I am new to swift UI and not sure how to use the VM's state publisher in SwiftUI.
The VM's state is a CurrentValueSubject
right now.
Thanks in advance.
Share Improve this question asked Nov 19, 2024 at 23:57 iOSeriOSer 2842 silver badges14 bronze badges 6 | Show 1 more comment1 Answer
Reset to default -1SwiftUI uses the @StateObject or @ObservedObject property wrapper to observe changes in the ViewModel. To enable this, conform your ViewModel to ObservableObject and use a @Published property to represent the state.
Since you already have a CurrentValueSubject in your ViewModel, you can connect it to a @Published property.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742390035a4434828.html
ObservabeObject
@Published
andassign
fromCombine
– lorem ipsum Commented Nov 20, 2024 at 1:22