Room ORM

Android ROOM is an abstraction layer over SQLite. An Android ROOM introduced in Google I/O 2017. Android ROOM implements ORM (Object — Relational Mapping) to store data.

So, What is ORM?

Object-relational mapping (ORM, O/RM, and O/R mapping) is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language.

Wikipedia reference:-

Advantages of an Android ROOM:-

To implement ORM in an Android, following are the famous libraries available.

1) Sugar ORM

2) GreenDAO

3) Realm

4) ORM Lite

5) ROOM.

Implementation:

Room Architecture

The three major components of Android Room are:

Step 1: Create Entity Class

In our project, User.kt is the Entity class.

Step 2: Create DAO Interface

In our project, UserDAO.kt is the DAO Interface.

Step 3: Create Database Class

Database contains the database holder and serves as the main access point for the underlying connection to the app’s persisted, relational data.

In our project, UserDatabase is the Database class.

Step 4: Create a UserDAO abstract function.

We are going to create an abstract function that will return the UserDAO Class.

abstract fun userDao: UserDAO

Step 5: Create a Companion Object.

companion object{}

As quoted from the Objects and Companion Page:

A companion objects is written within any class. And also, everything that is written inside the companion object will be basically visible to other classes.
companion object{}

Step 6: Making INSTANCE.

We are going to make our class as a singleton. A singleton class will only have one instance. But we will make the initial value for our instance variable as zero, thus null.

companion object{
private var INSTANCE: UserDatabase? = null
}

Step 7: Add Volatile Annotation.

Inside the companion object, we must add @Volatile annotation. Volatile make the rights to this field, immediately made visible to other threads.

companion object{
@Volatile
private var INSTANCE: UserDatabase? = null
}

Step 8: Create getDatabase function.

Then we are going to make a getDatabase function below it.

Step 9: Create Repository

A Repository abstracts access to multiple data sources.

The repository is not part of the architecture components, but it is a suggested practice for code separation and clean architecture.

Add a perimeter of the class to the DAO.

Step 9.1: Create variable readAllData. This variable will list all the users wrapped inside the livedata object. We are going to read the data from this user DAO.

Step 9.2: Create addUser function to add the user.

Use the DAO to access our addUser function from our userDAO.

And the function will have a suspend keyword since we are going to use coroutines in a viewmodel.

Step 10: Create ViewModel.

Heres an explanation of the ViewModel: ViewModel.

Create a ViewModel Class.

Step 10.1: Extends to AndroidViewModel.

Extends the ViewModel class to AndroidViewModel, a dependency from Android Lifecycle.

Android ViewModel is different from regular ViewModel since it contains application reference.

Step 10.2: Add the Application Reference.

Step 10.3: Create a variable named readAllData.

Create the variable inside the userViewModel class The readAllData will contains the lists of users, wrapped in the LiveData object.

Step 10.4: Create init block.

Init block is always first executed when the user ViewModel is called.

Reference:

An Introduction to Android ROOM
Android ROOM is an abstraction layer over SQLite.An Android ROOM introduced in Google I/O 2017. Android ROOM implements ORM (Object - Relational Mapping) to store data. So, What is ORM? Object-relational mapping (ORM, O/RM, and O/R mapping) is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages.
https://blog.nonstopio.com/an-introduction-to-android-room-e403b5c8ce43
Android Room Tutorial: Simplifying How You Work with App Data
Information is probably the most important resource that users trust with apps. For app developers, that information tells us who the user is, which empowers us to provide a good user experience (Within this Android Room tutorial, we will show you how you can work with this data more easily, while ensuring its integrity and security, all by using Android Room.
https://gorillalogic.com/blog/android-room-tutorial-simplifying-how-you-work-with-app-data/