Android Architecture Components — Room and Kotlin
By: Ritesh Singh
Tags: Architecture Components, Kotlin, Room, Android, AndroidDev

Now, coming to the implementation part.

How to use Room?

Add the required room dependencies.

Now, we need a table for our database with few required table columns. Let’s add an entity class.

Few points to be noted here * . The class should be annotated with Entity, this is how Room identifies, For each entity you create, a table is created with the associated Database. By default, Room creates a column for each field, but you can avoid this for few fields by using Ignore annotation. * . Each Entity, must define at least 1 primary key. You need to annotate the field with PrimaryKey annotation. * . By default Room uses the class name as the table name. You can give custom name by using tableName property.

There are many things which you can do like relationship between entities or creating a nested objects or multiple primary keys.

Check out here for more on the same.

Now, we need a DAO (Data Access Object), which will be used for accessing the database.

Few points to be noted here * . To interact or say access we need an interface annotated with DAO. This is how Room figures out. Each DAO includes methods that offer abstract access to your app’s database. * . By accessing a database using a DAO instead of a query builder or direct queries, you can separate out different components of your database architecture. * . DAO allows you to easily mock database access as your test app. * . A DAO can be either an interface or an abstract class. In case of an abstract class, it can optionally have an constructor that takes RoomDatabase as its only parameter. * . Room creates each DAO implementation at compile time.

You, can do so much more with the DAO, like passing parameters into the query, returning subsets of column, or passing a collection of arguments and more.

Check out here for more detail on the same.

Finally, we need a DatabaseClass, annotated with Database. The Database class establishes a logical grouping between the DAO interfaces. It also defines the required version number, which is used to track and implement database migrations.

Few things to be noted here * . You should follow the singleton design pattern when instantiating an AppDatabase object, as each RoomDatabase instance is fairly expensive, and you rarely need access to multiple instances. * . The class should be abstract and should extend RoomDatabase.

If you try running the above code with the created database above, your app will crash as the operation performed is on the main thread. By default, Room keeps a check of that and doesn’t allow operations on the main thread as it can makes your UI laggy.

You can avoid that by using AsyncTask or Handler or Rxjava with io schedulers or any other options which puts the operation on any other thread.

There is one more options, which allows you to do the operation on the main thread. You can use the same for testing but should avoid . To do that you can add allowMainThreadQueries() on the builder.

And you can also use inMemoryDatabase, for testing. This database will only exist with the application process, one the process dies, the database is cleared. All you need to do is use inMemoryDatabaseBuilder() instead of using databaseBuilder().