Search code examples
androiderror-handlingmvprx-java2android-mvp

MVP RxJava2 - managing Network error


I try to follow the MVP design pattern using RxJava2. I'm new with it. As I'm requesting data from an API, in my Presenter I call my Model like that:

mModel.getDataFromAPI()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(data -> {
        // Do something with the data
    }, throwable -> {
        // A wild error appears!
        // TODO: Display a dialog
    });

When there is an error, I want to display a dialog with a message to the user.

So what I was thinking is to create a displayErrorDialog(String message) on the View. But the problem is: I call the API a bit everywhere in the app, so it means that ALL my views will have to implement the method!

The dialog needs to be displayed by the View:

  • to follow the MVP design pattern
  • because I need the current Context to display the dialog.

I'm looking for a better solution to not have to implement the same method each time I have a new view.


Solution

  • You could have a BaseView that implements displayErrorDialog(String message) and then all of your views would extend from BaseView.

    If you are using interfaces you could do the same using a BaseInterface and then you would be able to call your base view methods from any presenter.

    I hope this helps!