Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MVP with RxAndroid + Retrofit

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

like image 618
Rinav Avatar asked Nov 20 '15 06:11

Rinav


People also ask

What is Android RxAndroid?

What exactly is RxAndroid? RxAndroid is a RxJava for Android extension that is only used in Android applications. RxAndroid added the Android-required Main Thread. We will need the Looper and Handler for Main Thread execution in order to work with multithreading in Android.

What is the difference between RxJava and retrofit?

Rx gives you a very granular control over which threads will be used to perform work in various points within a stream. To point the contrast here already, basic call approach used in Retrofit is only scheduling work on its worker threads and forwarding the result back into the calling thread.

What is Android MVP?

MVP (Model View Presenter) pattern is a derivative from the well known MVC (Model View Controller), and one of the most popular patterns to organize the presentation layer in Android Applications. This article was first published in April 2014, and been the most popular since then.

What is retrofit RxJava?

Retrofit lets us receive calls results with custom handlers instead of the normal Call object by using Retrofit Call adapters. This makes it possible to use RxJava Observables and Flowables here.


2 Answers

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.

like image 50
David Guerrero Avatar answered Oct 01 '22 18:10

David Guerrero


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.

like image 32
wnc_21 Avatar answered Oct 01 '22 18:10

wnc_21