Confused about error handling in network layer when implementing MVVM in android, How to notify user something is wrong?

Answer #1 100 %

You are in the correct path. I would place both methods in the NetworkDataSource. All the calls executed should call those methods to handle the errors.

The NetworkDataSource will return the NetworkResult to the repository, and it will return the result to the ViewModel.

As you say, you can use a LiveData to notify the Activity/Fragment. You can create an error data class:

data class ErrorDialog(
   val title: String, 
   val message: String

And declare a LiveData that will be observed from your view. Then when you receive notifications in your view, you can implement logic in a BaseActivity/BaseFragment to show a Dialog or Toast or whatever type of view to indicate the error.

You’ll also like:

© 2023 -