Room : problem and solution

Room : problem and solution

The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite.

Room database was announced in android architecture components to provide a layer over sqlite to simplify the sqlite integration for us, developers.

Room : The problem

One of the problems I have faced during my time developing apps using room was that I had to use LiveData as a return type for my queries to avoid making lots of AsyncTask implementations but in the end both ways are a headache to the developer

Not to mention when you try to implement a searching function which requires more than one execution to the same query with different parameters.

Methods, pros and cons

Both methods are very popular due to the limitations offered by Room itself, In my opinion the pros and cons of using the LiveData return type are :

Pros :

  1. Life-cycle awareness : The Android architecture components are lifecycle aware, meaning that there will be no views leaking or memory loss.
  2. No AsyncTask required : Since room database can execute async tasks via LiveData, then there won't be a need for the developer to implement an AsyncTask for it.
  3. Less code : Since LiveData and room makes this whole operation asynchronously, there won't be a need for those extra lines of codes for the AsyncTasks.

Cons :

  1. New instance, every time : One of the problems facing developers is that room database returns a new instance of LiveData class each time the method is executed, thus developers have to observe the new instance each time they execute the method.
  2. Creating new Observer instance each time : As a result to number 1, With each new LiveData class created, the developer must provide an observer instance to receive the data returned.

As for the AsyncTask implementations :

Pros :

  1. Free from the LiveData : Since you have implemented your AsyncTask, You don't have to return a LiveData instance, just return the data directly to the AsyncTask.
  2. No need to wait for observing : Since that LiveData is no longer a part of the code, There is no need to implement an extra Observer instance to observe and return the data.

Cons :

  1. Extra Coding : Since non-LiveData return type methods must be done in the background, You have to implement a method-specific AsyncTask class for it.
  2. No life-cycle awareness : One of the pros of LiveData is that it is life-cycle aware like I have explained above, So not using them may produce memory leaks.

My point of view ...

Is why not using both methods with a slight modification to get the benefits from both world ? After some research here is my point of view using a coding example I have developed.

Here is a simple User entity class for Room.

Then the UserDao that we will use to do CRUD operations on the entity.

And here is my UsersDatabase, So far I have done nothing out of the normal Room tutorial, So far so good ...

Now here is the difference, Here I am using A class called RoomLiveData which is a custom class for doing the background task and provide a LiveData class to observe, That way I get the background processing done AND get a single LiveData instance to always observe on.

But ... What is RoomLiveData ?!!

Simply, RoomLiveData is a wrapper class that uses a MutableLiveData instance to do an asynchronous operation using a Java thread and posts the value using PostValue method provided in the MutableLiveData class.

After a bit of testing and playing around with that class I have found a couple of problems facing me, for example, When I call the runAsync more than one time in a row, only the first value is returned, but when I put the runAsync in a button click, it executes perfectly.

This isn't the perfect solution yet for this problem, but it helped me finish a search function in a test app so I wanted to post it here so that someone may use it.

Also the code to the test project is open source, You can find it on GitHub here : https://github.com/tarekMohamedIT/RoomPlayground

TL;DR

Although Room is pretty great at making our life with SQLITE better, it isn't perfect yet, but I am sure that, by time, it will, so till that happens, developers have to find their ways around it's limitations.

I hope you have enjoyed this article and also I will be more than happy to see this project gets better by your contributions and suggestions, and if you want to ask about anything, I will be happy to reply as soon as I can.

Resources :

To view or add a comment, sign in

More articles by Tarek Mohamed

  • Assistant web

    Assistant web is my test for what I have learned in the last few weeks, The website's main role for now is to…

    1 Comment

Others also viewed

Explore content categories