How to use Rx-java with Room in android

Date : 08/04/2019

Prerequisite

If you need to use Rx-java in android, then add the following dependency in your gradle file as depend on the version of Rx-java you use.

// RxJava support for Room
implementation “android.arch.persistence.room:rxjava2:1.0.0-alpha5”

Need of Rx-java

Rx-java eliminates the boiler plate codes in java programming. It will make the program more readable. Another benefit is it will eliminate the task on separate threads and it write codes. Schedulers will take responsibility for running the tasks. Rx-java performs asynchronous calls to room. Some fundamental types of task are perform in room database as below.

Insert

Room integration with Rx-java allows the corresponding returns types.

1. completable – onComplete = as soon as insertion is done.

2. Single<long> or maybe<long> = where the value is emitted on onSuccess() which returns the row id in database.

3. Single<List<long>> or maybe<List<long>> = where the value is emitted on onSuccess() which returns the list of row id in database.

4. If error occur on inserting values in room the database, Maybe, Completable, Single all will emit the Exception in onError(). One the usage of Rx-java in room query attached here.

Use observeOn() to specify Scheduler on which the observer will observe the items emitted. Use subscribeOn() to specify Scheduler on which the observable operates.

Update/Delete

Room integration with Rx-java returns the corresponding types when update/delete the Room.

1.completable – onComplete = as soon as update/delete is done.

2.Single<Integer> or maybe<Integer> = where the value is emitted on onSuccess() which returns list of row id in database.

Use the observeOn() operator to specify the Scheduler on which it will observe the Observable and subsribeOn() to specify the Scheduler on which the Observable will operate.

Query

To get user from the database, we can write the following query in the data access object class.

The above method has two disadvantages

1. It is synchronous.

2. we have to call it by manually.

Rx-java provide Flowable, Maybe, Single are observes the database and delivers asynchronous results.

And it will keeps you out of Main thread. It also has a Scheduler to run every operation. It is your decision to mention the thread on observeOn().

For queries that returns maybe or single then make sure that you are calling subsribeOn() other than AndroiodSchedulers.mainThread().

Maybe

Here’s what happens:

  1. When there is no user in database then query returns no rows, Maybe will complete.
  2. When there is an user in database, then Maybe will trigger onSuccess() and it will complete.
  3. If the user is updated after Maybe was completed, nothing happens.

Single

Here are some scenarios:

  1. When there is no user in database then the query returns no rows, Single will trigger 0nError().
  2. When there is an user in the database, Single will trigger onSuccess().
  3. If the user is updated after Single was completed, nothing happens.

Flowable/Observable

Here’s how the Fowable/Observable behaves:

  1. When there is no user in the database, then query returns no rows, the Flowable will not emit anything.
  2. When there is an user in database, then Flowable will trigger onNext.
  3. At every time the user data is updated, then Flowable object will emit automatically, and allowing you to update the UI based on the latest data.

Conclusion

Rx-java will also assist you to testing the code. Hopefully in future,we will write about this.

Thank you for using pheonix solutions.

If You find this tutorial helpful? Share with your friends to keep it alive. .If you have doubts, feel free to leave a comment.

Leave a Reply