Skip to content

Instantly share code, notes, and snippets.

@robertlevonyan
Created March 4, 2021 07:02
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 robertlevonyan/16043589e2620cb827b2fbd713d7de08 to your computer and use it in GitHub Desktop.
Save robertlevonyan/16043589e2620cb827b2fbd713d7de08 to your computer and use it in GitHub Desktop.
@Database(entities = [RoomMovie::class], version = 1) // an array with entites and the current db version
@TypeConverters(value = [DateTypeConverter::class]) // an array of all type converters
abstract class AppDatabase : RoomDatabase() {
abstract fun movieDao(): MovieDao
companion object {
@Volatile
private var instance: AppDatabase? = null
fun getInstance(context: Context): AppDatabase =
instance ?: synchronized(this) {
instance ?: buildDatabase(context).also { instance = it }
}
private fun buildDatabase(context: Context): AppDatabase =
Room.databaseBuilder(context, AppDatabase::class.java, "MoviewDb")
.fallbackToDestructiveMigration() // destructively recreate database tables or you can write your own migration
.build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment