Reputation: 1156
In my ViewController (UIHostingController
) i have viewModel (reference type) shared between ViewController and its rootView. And viewModel has one property which is wrapped as @Published
.
@Published
it is not updating the UIViewController Code:
final class DashboardSceneViewController: UIHostingController<DashboardSceneView> {
var viewModel: DashboardSceneViewModel?
func setVm(response : Model){
viewModel.data = response. ///Here it should update DashboardSceneView But not updating
}
}
ViewModel:
protocol DashboardSceneViewModel {
var delegate: DashboardSceneViewDelegate? { get set }
var homeJson : HomeModel? {get set}
}
class DefaultDashboardSceneViewModel: DashboardSceneViewModel{
@Published var homeJson: Model?
}
View:
//Not getting redrawn on change in @published property change
struct DashboardSceneView: View {
var viewModel: DashboardSceneViewModel
var body: some View {
VStack {
if viewModel.homeJson != nil {
Text(viewModel.homeJson?.header ?? "")
}
Button("Tap"){
print()
}
}
}
}
Upvotes: 3
Views: 1670
Reputation: 257493
@Published does not update SwiftUI view by itself.
Your view model should be
ObservableObject
, and@ObservedObject
in viewSee this one for similar scenario with observable protocol https://stackoverflow.com/a/59504489/12299030
Upvotes: 1
Reputation: 5084
In order for @Published
to update the UI, it's parent(DefaultDashboardSceneViewModel
) should be conform to ObservableObject
:
protocol DashboardSceneViewModel: ObservableObject {
Now, for ObservableObject
to bind to your View
, you need to mark it with @StateObject
. However, since DashboardSceneView
did not initialize viewModel
you mark it with @ObservedObject
:
@ObservedObject var viewModel: DashboardSceneViewModel
Upvotes: 1