I currently have an app that's fetching data from an API, In the root view (let's call it Home) everything works as expected, in the second view (let's call it User View) everything works as expected but now on the third view (Team View) the ObservedObject
for this view only is not working.
The strangest part is that if the user navigates directly to the Team View, again every thing works as expected.
Each view has it's own ObservedObject
has the data being loaded belongs only to that view
The navigation between each view is made by the NavigationLink
Heres an exemple of how I'm doing the loading and navigation.
struct HomeView: View {
@ObservedObject var viewModel = HomeViewModel()
var body: some View {
VStack {
NavigationLink(destination: UserView(userId: viewModel.userId))
NavigationLink(destination: TeamView(teamId: viewModel.teamId))
}
}
}
struct TeamView: View {
@ObservedObject var viewModel = TeamViewModel()
@State var teamId: String = ""
var body: some View {
Text(viewModel.name)
.onAppear() { viewModel.loadData(id: teamId) }
}
}
struct UserView: View {
@ObservedObject var viewModel = UserViewModel()
@State var userId: String = ""
var body: some View {
VStack {
Text(viewModel.name)
NavigationLink(destination: TeamView(teamId: viewModel.teamId))
}
.onAppear() { viewModel.loadData(id: userId) }
}
}
From the example you can see that the function to load the data is in the view model and is loaded when the view appears
Everything works just fine but when I reach the 3rd level in the stack the data does not get updated in the view. I thought It might be the thread but I'm using DispatchQueue.main.async
when the fetch is complete.
All necessary variables on the Model are marked as @Published
In sum the following flows work
HomeView -> TeamView
HomeView -> UserView
But this one on the last view it does load the data but it does not update the view
HomeView -> UserView -> TeamView