-
-
Save im-o/52703f8d7175ca2ba872c8a8d287a289 to your computer and use it in GitHub Desktop.
NoteDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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