I have a view and a viewModel that should update the ListView when users are added to the user array. I can verify that users are being added, yet the ObservedObject is not updating.
I have a search bar that lets you search users and then updates user array in the ViewModel which is supposed to update the View but it doesn't.
ViewModel
class UsersViewModel: ObservableObject {
@Published var users: [User] = []
@Published var isLoading = false
var searchText: String = ""
func searchTextDidChange() {
isLoading = true
API.User.searchUser(text: searchText) { (users) in
self.isLoading = false
self.users = users
}
// confirmed that users has data now at this point
}
}
View
struct UsersView: View {
@ObservedObject var usersViewModel = UsersViewModel()
var body: some View {
VStack() {
SearchBarView(text: $usersViewModel.searchText, onSearchButtonChanged: usersViewModel.searchTextDidChange)
// Not getting called after usersViewModel.users gets data
if (usersViewModel.users.count > 0) {
Text(usersViewModel.users[0].username)
}
}
}
}