Integration of Realm, RxJava and Retrofit in one project | Develandoo | Blog

Integration of Realm, RxJava and Retrofit in one project

Integration of Realm, RxJava and Retrofit in one project This article is for giving the concept of integrating three most known and used libraries by Android developers – Realm, rxJava and Retrofit. The idea behind this is to generate more precise and clean code, by tying RealmObjects with Retrofit, and handling Retrofit result by rxJava.  In case if you are not familiar with early mentioned libraries, you will better to visit official sites for more detailed information. One of the problems that can be solved by this technique is the satisfaction of the open close principle of OOP. Namely in usual case we are forced to create POJO, database ORM, add Model-to-Database-Mapping classes, and unfortunately in case of changes all three corresponding classes should be rewritten. While in case of correct use of third party libraries we can reduce the amount of unnecessary code and make it nicely readable. How should we achieve this? Step 1: Create Database Helper class

Step 2: Add DataBase Configuration into application.

Step 3: Now our Database should be supplied with realmObjects. Generally our projects are compelled to have parcelable objects, due to project architecture requirements. I am referring to this because Realm library did not fully support native android Parcelable interface. In case of using just simple realmObjects, you are okay with it, but if you want to store list of the realmObjects and make them parcelable it will throw an exception. Therefore, in any case I am suggesting to use Parceler library. For simple realmObject use @Parcel annotation like in example.  Note all classes that extend RealmObject will have a matching RealmProxy class created by the annotation processor. Parceler must be made aware of this class (this class is not available until the project has been compiled at least once).

To make a realmList parcelable we should create MyModelListParcelConverter, which extends RealmListParcelConverter.

Step 4: Create APIHelper class to configure Retrofit.

Step 5: Add your request with Observable rxJava class, so that we can use its benefit on stage of getting response.

Step 6: Now let’s see how we can easily handle retrofit response with RxJava – Observable help.

Step 7: Insert the result into the database. One of the benefits of using Realm is the easy way of writing queries. For example let’s set tree function insert(), getMyModel(String name), and getAll().

As we saw the advantages of Retrofit – automatized process of getting the response parsed into your model objects, Realm – work with “normal” objects, instead of having to use SQLite, and RxJava – powerful Observables, gives an opportunity to extremely reduce the number of classes, clean the code, and made it pleasurable for writing.  Moreover, these three libraries together make working with data from a backend quick and easy.

  • Topics:
  • Android

Top Stories

High Five! You just read 2 awesome articles, in row. You may want to subscribe to our blog newsletter for new blog posts.