First Time With Android Room Database

Bright Ahedor
3 min readJun 26, 2020

--

Room Dao sample

I’ve been a programmer for about 7 years now. I have tried several persistence libraries in android back in the days. I have tried realm my most favorite up until now, I have used Android Sugar, ORMLite, GreenDao, and a lot more if I may say. I have built lots of applications so I have tried several of them. In the past couple of days, I decided to give room a try because I started hearing a lot about it. And this is my experience with room database.

Why I Like Room

Here are a few reasons why I like room database.

  1. Same object classes, with room I can use the same object to map my Rest API response data and also throughout my application. This is not so with some libraries. You will have to write separate classes for the two cases.
  2. It is from Google, we mostly tend to use google libraries because you are sure of constant support. You don't want to use a library that the developers won't be able to support in this fast-changing android world.
  3. Simple Interface queries, at least for room you get access to your saved data with simple SQLite queries you already know using annotations.
  4. Works fine with LiveData architecture approach released by google. If you have been following trends, you will notice the new trend and changes in android and approach to building apps. It is important that you use libraries that easily works with all these new developments.
  5. Easy parceable implementations, in android we love to parse a whole object through intents, and to do this you may have to implement the parceable interface on the object you are parsing. With room you can use same object for that. Unlike room for some database libraries that seem to change the class signatures, it hard to do this.

Setting Up Room Database

Step one, you have to add the room dependencies in your project. Be sure to add it to the right place. If you are using the old way of building apps. then just go ahead and add it to your build.gridle at the app level but in case you are causing the clean architecture approach then possibly should be in your data level gradle. Find the example below

Create a model or a table to hold the data. If you are using kotlin then a simple data class should be fine. All you need to do is annotate it as an entity. See below

Create a DAO, a data access object. This is simply an interface that defines how data should be accessed from your room database. It is a simple interface class with annotations. See the sample for the product class below.

Finally, register all the DAO classes in the database class and create an instance of the database to be used in your app. A sample is seen below

This class exposes all the entities you can access on the database. To close all these you need to create a singleton of the database in your app. If you are familiar with android you will know that we need only one instance of the database running at all times. This serves as the doorway to our database. This class also specifies all migration requirements in cases of change in your database structure. It also exposes the database classes for use in our app. if you are building using a simple architecture, doing this your application class should be fine. Something like this will do.

I hope this helps you get started with room database. With this, you can call the instance to set up your database and also use it anywhere in your app.

--

--

Bright Ahedor
Bright Ahedor

Written by Bright Ahedor

Aeronautics Engineer and Founder

No responses yet