10

Recently I started reading a lot about MVP and I want to get into practicing my projects with it.

However I am not able to correctly understand where should Rx + Retrofit code go? I think it should be in Model Layer via Interactors but still can someone share some light on this?

Also what happens with the RX callback? the onNext(), onCompleted() and onFailure() passes data back to Presenter or do we implement listeners and then pass it on to Presenter?

I also want to persist data (Realm/StorIO) when I get it in onNext(), So again pass it to another DataLayer or where should it Go?

Also should we decouple Rx callbacks further?

I am following this post https://davidguerrerodiaz.wordpress.com/2015/10/13/a-brief-introduction-to-a-cleaner-android-architecture-the-mvp-pattern/

and this seperate github repo from antonioleiva.com https://github.com/antoniolg/androidmvp

Rinav
  • 2,527
  • 8
  • 33
  • 55
  • I have no any experience in Rx and Retrofit, but they seem to be best live in M. Consider P and V as a sort of an "add-on" to interact with user, so their responsibility is just to notify a user and listen to interactions. No business logic in P and V in short. Also consider your M as an "open gate" to outer codebase that can be re-used elsewhere, let's say in a non-user-interaction application (a system service, for example), or even other environments like GWT. This brings a certain complexity via interfaces of different layers, but this makes your system loose coupled. – Lyubomyr Shaydariv Nov 20 '15 at 07:15

2 Answers2

5

As you pointed the RxJava functionality defines a use case of your model layer so it would be placed in an interactor of this layer. You can create a different interactor for each use case. Let's say you are pulling a list of users from your server, this would be a use case and an interactor that will have the RxJava/Retrofit Observable.

Then you will have a Presenter with an Observer in it which is interested in this users list, so it will be subscribed to that Observable.

And finally when this Observer in has all the data from the Observable (onCompleted), it will transform this data (if needed it) and pass to the View which will be just in charge of display it.

David Guerrero
  • 1,080
  • 7
  • 7
5

There is awesome post explaining mvp. Rx is just additional tool for it.

http://hannesdorfmann.com/android/mosby-playbook/

there is deep explanation and source code with example.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
wnc_21
  • 1,751
  • 13
  • 20