Android Room using Kotlin, Coroutines, ViewModel, LiveData Android 27.09.2019

Room is a part of the Android Architecture components which provides an abstraction layer over SQlite which allows for a more robust database acces while still providing the full power of SQlite. You can read about Room in details here.

Room 2.1 (SQLite Persistence Library) added the support for coroutines. Now you can add the suspend keyword to DAO class methods and ensures that they are not executed in the MainThread.

Coroutines are light-weight threads. They are launched with launch coroutine builder in a context of some CoroutineScope.

LiveData is an observable data holder, part of the Android Jetpack. LiveData considers an observer, which is represented by the Observer class, to be in an active state if its lifecycle is in the STARTED or RESUMED state. LiveData only notifies active observers about updates.


First, we need to add the needed dependencies that will enable us to use the Room library. We can do so with some simple lines in the build.gradle (Module:app) file. Last version of Room here. Last version of Lifecycle here.

apply plugin: 'kotlin-kapt'
dependencies {
    def lifecycle_version = "2.1.0"
    def room_version = "2.2.0-rc01"
    def kotlin_version = "1.3.2"

    // Kotlin coroutine dependencies
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_version"

    // Room architecture component dependencies
    implementation "$room_version"
    kapt "$room_version"
    implementation "$room_version"

    //Live data and life cycles
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"


The Room library consists of 3 major components: Entity, DAO (Database Access Object), Database.

The Entity represents a table within the database and has to be annotated with @Enity. Each Entity consist of a minimum of one field has to define a primary key.

@Entity(tableName = "movie_items")
data class MovieEntity(
    @ColumnInfo(name = "title") var title: String,
    @ColumnInfo(name = "year") var year: Int
) {
    @PrimaryKey(autoGenerate = true)
    var id: Int = 0

In Room you use DAO to access and manage your data. The DAO is the main component of Room and includes methodes that offer access to your apps database it has to be annotated with @Dao. DAOs are used instead of query builders and let you seperate differend components of your database e.g. current data and statistics, which allows you to easily test your database.

interface MovieDao {
    @Query("SELECT * FROM movie_items ORDER BY title ASC")
    fun getAll(): LiveData<List<MovieEntity>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(vararg movies: MovieEntity)

    suspend fun delete(movie: MovieEntity)

    suspend fun update(vararg movies: MovieEntity)

Serves as the database holder an is the main accespoint to your relational data. It has to be annotated with @Database and extents the RoomDatabase. It also containes and returns the Dao (Database Access Object).

@Database(entities = [MovieEntity::class], version = 1)
abstract class MovieDatabase: RoomDatabase() {
    abstract fun movieDao(): MovieDao

    companion object {
        private var INSTANCE: MovieDatabase? = null

        fun getInstance(context: Context): MovieDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance

            synchronized(MovieDatabase::class) {
                val instance = Room.databaseBuilder(

                INSTANCE = instance
                return instance


This is a class where we will check whether to fetch data from API or local database, or you can say we are putting the logic of database fetching in this class.

class MovieRepository(private val movieDao: MovieDao) {
    val allMovies: LiveData<List<MovieEntity>> = movieDao.getAll()

    suspend fun insert(movie: MovieEntity) {


This is the part of lifecycle library; this will help you to provide data between repository and UI. This survives the data on configuration changes and gets the existing ViewModel to reconnect with the new instance of the owner.

class MovieViewModel(application: Application): AndroidViewModel(application) {
    private val repository: MovieRepository
    val allMovies: LiveData<List<MovieEntity>>

    init {
        val movieDao = MovieDatabase.getInstance(application).movieDao()
        repository = MovieRepository(movieDao)
        allMovies = repository.allMovies

    fun insert(movie: MovieEntity) = viewModelScope.launch {


class RoomActivity : AppCompatActivity() {
    private lateinit var vm: MovieViewModel
    private val TAG = "TAG"

    override fun onCreate(savedInstanceState: Bundle?) {

        vm = ViewModelProviders.of(this).get(

        vm.allMovies.observe(this, Observer { items ->
            if (items.isEmpty()) {
                vm.insert(MovieEntity("Movie 1", 2001))

            Log.d(TAG, "ITEMS: $items")

SQLite debug tools

  • Stetho is a debug bridge for Android that allows you to connect to a device and to inspect your app using Chrome Developer Tools.
  • SQLScout is a plugin for Android Studio and Intellij IDEA which allows you to connect to SQLite databases, browse tables and schemes.
  • Liya
  • SQLPro for SQLite