Android Room Persistence Library Sqlite Mapper with Kotlin

In this tutorial, we will learn step by step to use the Android Room a persistence SQLite ORM mapper. As we have already practiced in my last tutorial with GreenDAO which is SQLite wrapper for fast ORM. I would be recommended to checking this post Exploring GreenDAO ORM database with Reactive RxJava.

Google I/O 2017 has introduced the new database persistence library called Room as ORM like GreenDAO and ORMLite. There are 3 major components in Room.

  1. Database
  2. Entity
  3. DAO

Database: Database is a component that creates the database holder. The annotation defines the list of entities, and the class’s content defines the list of data access objects (DAOs) in the database. It is also the main access point for the underlying connection.

Entity: This component represents a class that holds a database row. For each entity, a database table is created to hold the items.

DAO: This component represents a class or interfaces as a Data Access Object (DAO). DAOs are the main component of Room and are responsible for defining the methods that access the database.

For more detail Please check the official documentation.

I had been using Kotlin prior to the I/O announcement. Having google fully commit it to as a first class citizen on Android was very encouraging. Here is the recommended post to check Kotlin.

Ok, Let’s integrate the Room library in one of the sample projects with Kotlin in reactive nature by RxJava2. Very first thing we need to add relevant dependencies in the build.gradle file.

In project level of build.gradle file we need to add maven google path to download the dependency.

Now all setup is ready, let’s create the database entity.

Now let’s create a DAO class.

Now create an instance of a database.

Now let’s create the instance of the database to observing with Live data.

Let’s create an interface for what are the operations required in the project and implement those locally.

Ok, Let’s implement those.

Please add database creator into MainApplication.

Ok all done, We can call the insert operation to insert data into the table and get details from the database table.

Wrapping Up:  This is the good implementation of Android Room with Kotlin. Room introduced in new Android Architecture component. But this is still in alpha, So it might change implementation. If you wondering Kotlin for android then I would be recommended to check all this post of Kotlin Category. You can get the full source code from Github.

Please do subscribe your email to get every newsletter from this blog and if you feel that this post helps you then do not forget to share and comment below.

You can learn all articles of MobologicPlus from the android app, which is available to download below.

[appbox googleplay  mobi.androapp.mobologicplus.c7929]

Happy coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.