Skip to content

Instantly share code, notes, and snippets.

@ma-za-kpe
Created November 13, 2019 07:53
Show Gist options
  • Save ma-za-kpe/d17e86827be09e853a46a5384c353cb6 to your computer and use it in GitHub Desktop.
Save ma-za-kpe/d17e86827be09e853a46a5384c353cb6 to your computer and use it in GitHub Desktop.
Can anyone please explain this method to my(as if explaining to a dummy)
// Annotates class to be a Room Database with a table (entity) of the Word class
@Database(entities = arrayOf(Word::class), version = 1, exportSchema = false)
public abstract class WordRoomDatabase : RoomDatabase() {
abstract fun wordDao(): WordDao
companion object {
// Singleton prevents multiple instances of database opening at the
// same time.
@Volatile
private var INSTANCE: WordRoomDatabase? = null
fun getDatabase(context: Context): WordRoomDatabase {
val tempInstance = INSTANCE
if (tempInstance != null) {
return tempInstance
}
synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
WordRoomDatabase::class.java,
"word_database"
).build()
INSTANCE = instance
return instance
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment