You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Problem
When I press the "Search User" button with an empty phone number, I correctly see an error message displayed in the UI (e.g., "Phone number is required").
On subsequent presses of the same button, the ViewModel's state is updated correctly, as verified through logs, but the SwiftUI UI does not reflect the updated state.
Expected Behavior
The SwiftUI UI should update to reflect the latest changes in the ViewModel's state whenever the state is modified.
The text was updated successfully, but these errors were encountered:
On subsequent presses of the same button, the ViewModel's state is updated correctly, as verified through logs, but the SwiftUI UI does not reflect the updated state.
Hi! I am assume you mean that SwiftUI doesn't show the loading state, correct?
In that case this behaviour is expected. viewModelScope.launch will launch a new async job on the Main dispatcher / thread.
Everything inside the launch up until the delay will run uninterrupted.
Changing the state value only notifies SwiftUI that the state has changed.
But only once the main thread is free SwiftUI will/can rerender the view.
So adding a delay before the phoneNumber check would allow SwiftUI to update the view.
Similarly launching the job on another dispatcher (such as Default) would also allow SwiftUI to update the view.
However there is no guarantee that SwiftUI will update the view faster than you are updating the state (unless you are using a significant delay).
I am encountering an issue where the UI in my SwiftUI application does not update correctly after the ExterUserViewModel's state is modified.
Below is the relevant code for my ExterUserViewModel:
open class ExterUserViewModel() : ViewModel() {
}
in swiftui
@StateViewModel var viewModel = ExterUserViewModel()
var body: some View {
VStack {
if viewModel.exterUserState.isLoading {
ProgressView("Loading...")
} else if let error = viewModel.exterUserState.error {
Text("Error: (error)")
} else {
Text("User search completed")
}
}
Problem
When I press the "Search User" button with an empty phone number, I correctly see an error message displayed in the UI (e.g., "Phone number is required").
On subsequent presses of the same button, the ViewModel's state is updated correctly, as verified through logs, but the SwiftUI UI does not reflect the updated state.
Expected Behavior
The SwiftUI UI should update to reflect the latest changes in the ViewModel's state whenever the state is modified.
The text was updated successfully, but these errors were encountered: