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
.
- On receving response to viewcontroller from api call.
- updating viewModel with received data from server
- here after setting value to
@Published
it is not updating the UI
ViewController 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()
}
}
}
}