Basic Implementation of Room Database With Repository and ViewModel | Android Jetpack
By: Caner Gures
Tags: Android App Development, Android, AndroidDev, Room Database, Kotlin

the image is taken from here

In this article, I will explain the Room Persistence Library, and we will try to implement our first database.

Android Jetpack is a collection of components for Android development. Room database is a component for databases. Let me explain the Room in detail. We will be using Kotlin and Android Studio.

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 allows you to create a data cache to keep the application’s data. The cache serves as a single source of data and allows you to keep consistent data to use. That component works regardless of whether users have an internet connection.

Advantages

Compile-time verification of SQL queries

Less boilerplate code

Easily integrated with other architecture components

Official video of the Android Jetpack Room Persistence Library

Room has three main components:

Entity

DAO

Database

1. Entity

Represents a table within the database. Room creates a table for each class that has @Entity an annotation, and the fields in the class correspond to columns in the table. Therefore, the entity classes are small model classes that don’t contain any logic.

2. DAO

DAO’s are responsible for defining the methods that access the database. DAO’s are places where we add our queries.

3. Database

Contains the database and serves as the main access point for the underlying connection to your app’s data.

Implementation of RoomDB

Adding coroutines, lifecycle, and roomdb dependencies to build.gradle .

//Coroutines

api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9"

api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"

//Lifecycle Components

implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"

implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"

//Annotation processor

kapt "androidx.lifecycle:lifecycle-compiler:2.2.0"

//Room

implementation "androidx.room:room-runtime:2.2.5"

kapt "androidx.room:room-compiler:2.2.5"

implementation "androidx.room:room-ktx:2.2.5"

Creating a Model Class

Annotate the class with @Entity and use the tableName property to set the name of the table.

and use the property to set the name of the table. Set the primary key by adding the @PrimaryKey annotation to the correct fields.

annotation to the correct fields. Set the name of the columns for the class fields using the @ColumnInfo(name = “column_name”) annotation. If you have the correct column name, you can skip this annotation.

Creating DAO’s (Data Access Objects)

DAO’s needs to be created with annotation @Dao and an interface.

Creating a Database

We need to create an abstract class that extends the Room Database.

Creating Repository

A repository class abstract access to multiple data sources. The repository is not part of the Architecture Component libraries but is a suggested best practice for code separation and architecture.

Creating a View Model

ViewModel is like a bridge between UI and repository. The role of the VM is to provide data to UI.

After creating these classes, you can use the View Model in your activity and do several operations. Also, you can add different operations to View Model for using it as the update and delete as we coded in the repository above.

I hope this article helps you to understand Room DB and it’s implementation. See you in another article. Bye!