Skip to content

Instantly share code, notes, and snippets.

@behrends
Last active November 7, 2019 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behrends/1549b198884da2a52d681ca8128a8dad to your computer and use it in GitHub Desktop.
Save behrends/1549b198884da2a52d681ca8128a8dad to your computer and use it in GitHub Desktop.
package com.example.roomexample
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
@Database(entities = [Person::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun personDao(): PersonDao
companion object {
@Volatile private var instance: AppDatabase? = null
@Synchronized
fun getDatabase(context: Context): AppDatabase {
if(instance == null) {
instance = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"people.db"
).build()
}
return instance!!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment