Skip to content

Instantly share code, notes, and snippets.

@im-o
Last active October 25, 2020 12:46
Show Gist options
  • Save im-o/52703f8d7175ca2ba872c8a8d287a289 to your computer and use it in GitHub Desktop.
Save im-o/52703f8d7175ca2ba872c8a8d287a289 to your computer and use it in GitHub Desktop.
NoteDatabase.kt
@Database(
entities = [Note::class],
version = 1,
exportSchema = false
)
abstract class NoteDatabase: RoomDatabase() {
abstract fun getNoteDao(): NoteDao
companion object {
private const val DB_NAME = "note_database.db"
@Volatile private var instance: NoteDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance ?: synchronized(LOCK){
instance ?: buildDatabase(context).also {
instance = it
}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
NoteDatabase::class.java,
DB_NAME
).build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment