I’m trying out Android ViewModels for the first ti...
# android
s
I’m trying out Android ViewModels for the first time. It seems like the general practice is to have a
Flow
to watch the data changes from within the ViewModels, and then functions to pass events into the ViewModels, and to start loading etc. How should errors be handled when a network request fails? Should it be part of the observed Flow type, or is there a better way to handle this?
f
depends how you want to handle errors. If you want your error state to be
sticky
, i.e., have the UI in an error state until the user does something, then it should be part of the state. If you want your error to be transient, for instance to show a snackbar, then you could have a
flow
for events that the UI subscribes to and is used to trigger these transient events. If you use compose, then the recommendation is to have the error (or just events in general) as part of the state, then observe those in the UI using a
LaunchEffect
on the list of events and notifying the viewmodel when one has been consumed.