How to Use Singleton Pattern for Room Database in Android?

15 Oct, 2021
Prerequisite:
Room database in Android
Singleton pattern
Room is one of the Jetpack Architecture Components in Android. This provides an abstract layer over the SQLite Database to save and perform the operations on persistent data locally. This is recommended by Google over SQLite Database although the SQLite APIs are more powerful they are fairly low-level, which requires a lot of time and effort to use. But Room makes everything easy and clear to create a Database and perform the operations on it. The singleton pattern is one of the simplest design patterns. Sometimes we need to have only one instance of our class for example a single DB connection shared by multiple objects as creating a separate DB connection for every object may be costly. Similarly, there can be a single configuration manager or error manager in an application that handles all problems instead of creating multiple managers. In short, we use the Singleton pattern when we need to instantiate a single instance of a class. We define a class in such a way that only one instance can be created.
A few of the examples where we can use Singleton Pattern in Android are:
When we are using the Room database, We need only one instance of the Room Database class throughout the app. Having the number of instances of a Database can lead to memory leaks. Therefore we use the singleton patterns in the Database.
In MVVM architecture, When we are creating Repository, We only need one Instance as nothing changes in the Repository class throughout the execution of the app.
There are other situations as well where we prefer to use the Singleton pattern. In this article, we will focus on How to use the Singleton pattern in android. As we have got the idea behind using the Singleton pattern in the Database, Now we shall further move on for the implementation.
Here, We will Implement the Room database(assuming a database to store contacts) using the Singleton pattern.
First, We will define the Data Entity.
It will be a data class, let’s give it a name Contact.kt
Refer to the below code for reference.
Kotlin
import androidx.room.Entityimport androidx.room.PrimaryKey  @Entity(tableName = "contact")data class Contact(    @PrimaryKey(autoGenerate = true)    val id: Int,    val name: String,    val phoneNum: String)
(autoGenerate = true) is used to auto-increment to id whenever a new data is added.
Then we need to create Dao.
Dao is an interface, So no need to define methods inside it. Room takes to care for implementation of these methods.
Dao is used to accessing the data object in the database.
Refer to the below code for reference.
Kotlin
import androidx.lifecycle.LiveDataimport androidx.room.Daoimport androidx.room.Insertimport androidx.room.Query  @Daointerface ContactDao {    @Insert    fun insert(contact: Contact)      @Query("Select * From contact")    fun getContact():List<Contact>}
Now we will create the database class, it is the main access point to the application’s persisted data.
It is an abstract class, It inherits the RoomDatabase. Here we implement the singleton pattern.
Refer to the below code for reference.
Kotlin
import android.content.Contextimport androidx.room.Databaseimport androidx.room.Roomimport androidx.room.RoomDatabase  @Database(entities = [Contact::class], version = 1)abstract class ContactDatabase : RoomDatabase() {    abstract fun contactDao(): ContactDao      companion object {        private var INSTANCE: ContactDatabase? = null        fun getDatabase(context: Context): ContactDatabase {            if (INSTANCE == null) {                synchronized(this) {                    INSTANCE =                        Room.databaseBuilder(context,ContactDatabase::class.java, "contact_database")                            .build()                }            }            return INSTANCE!!        }    }}
We have used a companion object to directly access the getDatabase( ) method while creating an instance of the Database class.
We have used a synchronized block to prevent multiple calls at a time from different threads.
So, this companion object is common whenever you want to use the Singleton pattern in the Room database.
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.