paint-brush
Yet another MVP article — Part 4: RxJava and RxAndroid know how to responseby@mohsenoid
5,612 reads
5,612 reads

Yet another MVP article — Part 4: RxJava and RxAndroid know how to response

by Mohsen MirhoseiniNovember 28th, 2016
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<em>Implementing a sample Android Application using </em><strong><em>MVP</em></strong><em>, </em><strong><em>RxJava</em></strong><em>, </em><strong><em>Dagger 2</em></strong><em>, </em><strong><em>Retrofit2</em></strong><em>, </em><strong><em>Test</em></strong><em>, and all the brand new modern methods and libraries</em>

People Mentioned

Mention Thumbnail
featured image - Yet another MVP article — Part 4: RxJava and RxAndroid know how to response
Mohsen Mirhoseini HackerNoon profile picture

Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern methods and libraries

Previously on this article…


Yet another MVP article — Part 3: Calling APIs using Retrofit_Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com

Let’s think that you have studied part1, part2 and part3 of this article which means you know everything about the sample project’s modules and structure, also get more familiar with Dagger usage in wiring up MVP layers and know the use of Retrofit for connecting to network APIs.

now let’s continue…

RxJava is a revolution, truth or fiction!

During whole my career as a software developer I can say that RxJava is one of the best changes after Lambda expressions in Java 8. By following the recipes of mixing these two together you can develop software like a magician.

According to RxJava official website reactivex.io, it is “An API for asynchronous programming” which is always required in Android development.

Fortunately, this library documents on its website are perfect and you can find what ever you want to know with lots of graphs.

…, so let’s get back to our sample project:

In this project, RxJava is mostly being used for retrieving data from the Model layer, and the Presentation layer calls View layer’s methods to hand the response.

Observables are being provided with Retrofit library as a result of calling API methods and being observed by Presentation layer of MVP.

The magic part about RxJava is its possibility of being observed and subscribed in different threads. Cause we always hand results to Android UI thread (i.e. MainThread) so we need a link to this thread inside core module from app module.

How to specify Schedulers from Android to the core!

Schedulers in RxJava are threads in java. To hand response from API to View layer, you have to push it into Android UI MainThread.

To specify it we use observeOn method of RxJava.

RxJava has an extension for Android called RxAndroid which provide android threads for using with RxJava. To make this connection between core and app modules possible, app module implements SchedulerProvider interface from core within AppSchedulerProvider class which has both methods (mainThread/backgroundThread) implemented:

“Take care of the wrong abstraction”, — Novoda

, and finally by some Dagger injection, we can have AppSchedulerProvider as its parent (SchedulerProvider) inside our presenter class, which is another example of Liskov substitution in SOLID principle foot print in MVP.

RxJava MAP operation comes for help…

RxJava has many useful operators but I think MAP is the most used operator.

Take a look at this part of code inside SearchPresenterImpl which do the character search:

The response from API is not always what we want to hand to View layer, and even may some Database caching is required, therefore, using the MAP operator we can do some tricks before UI knows.

There are four MAP operators applied to API response inside SearchPresenterImpl:

  • The 1st map, check if the response code from API is Okay and if not an exception will throw later. Make sure that you use ‘Exceptions.propagate’ method, otherwise you have to handle the exception right there using try/catch statement!!
  • The 2nd map, check if is there any result for the character being searched and if not an exception will throw later…
  • The 3rd map, really maps response model into the database model using Mapper class.
  • and finally, the last map, insert response into the database for a further cache.

Using RxJava, all of these manipulations over API responses are possible in one line of code!

… and remember that all mapping things happen inside background thread, cause we specified it using subscribeOn inside SearchInteractorImpl class, and this is how it would never interrupt the MainThread to cause any lag or even ANRs.

Where else?

RxJava is also being used in Adapters for notifying items in list click, which I think is easy to understand and you can take a look.

That’s it for now…

Please clone the project repo from GitHub and get more familiar with it because in next part I will explain more about writing test using all dagger things we have done.

I look forward to your comments and helping for more improvements with this article.

Share this article if you think it is useful, and follow me for more articles Mohsen Mirhoseini.

To be continued…


Yet another MVP article — Part 5: Writing Test using a mixture of Dagger and Espresso_Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com