Skip to content
Related Articles
Open in App
Not now

Related Articles

Room Database with Kotlin Coroutines in Android

Improve Article
Save Article
  • Last Updated : 28 Jan, 2022
Improve Article
Save Article

This project will be used for the implementation phase. If you have not yet completed the project, you should do so and then return. To keep things simple, the project employs a basic MVVM architecture. The complete code for the implementation mentioned in this blog can be found in the project itself. First, we must configure the Room Database’s dependencies as shown below:

implementation ""
kapt ""
implementation ""

Don’t forget to include the Kotlin Annotation Processing plugin in your app-level gradle file.

apply plugin: 'kotlin-kapt'

Create the entity that will be our data class in Room Database, for example, Course.


data class GFG(
    @PrimaryKey val courseID: Int,
    @ColumnInfo(name = "courseName") val name: String?,
    @ColumnInfo(name = "courseID") val email: String?,
    @ColumnInfo(name = "coursePrice") val avatar: String?

For this User, we must create the Dao required for the Room Database, which we will call CourseDao.


interface CourseDao {
    @Query("SELECT * FROM Course")
    suspend fun getAll(): List<Course>
    suspend fun insertAll(Courses: List<Course>)
    suspend fun delete(Course: Course)

Please keep in mind that we’ve used the suspend keyword to support Coroutines, so we can call it from a Coroutine or another suspend function. We must now create the AppDatabase that will extend RoomDatabase.


@Database(entities = [Course::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun CourseDao(): CourseDao

Following that, we will require a DatabaseBuilder that is a Singleton.


object DatabaseBuilder {
    private var INSTANCE: GfgDatabase? = null
    fun getInstance(context: Context): GfgDatabase {
        if (INSTANCE == null) {
            synchronized(GfgDatabase::class) {
                INSTANCE = buildRoomDB(context)
        return INSTANCE!!
    private fun buildRoomDB(context: Context) =

Then, we’ll make a DatabaseHelperImpl implement the DatabaseHelper.


class DatabaseHelperImpl(private val gfgDatabase: GfgDatabase) : DatabaseHelper {
    override suspend fun getCourses(): List<Course> = gfgDatabase.CourseDao().getAll()
    override suspend fun insertAll(Courses: List<Course>) = gfgDatabase.CourseDao().insertAll(Courses)

Again, we used the suspend function so that we could call it from a coroutine or another suspend function. We can pass this instance wherever it is needed, such as to the ViewModel, and make the query to get the users from the database as shown below


class RoomDBViewModel(private val gfgApiHelper: GfgApiHelper, private val dbHelper: DatabaseHelper) :
    ViewModel() {
    init {
    private fun fetchCourses() {
        viewModelScope.launch {
            try {
              val CoursesFromDb = dbHelper.getCourses()
              // here you have your CoursesFromDb
            } catch (e: Exception) {
              // handler error

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!