2020-12-01 16:24:12 +01:00

54 lines
1.6 KiB
Kotlin

package com.example.todoapp.data
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.example.todoapp.data.models.ToDoData
@Database(entities = [ToDoData::class], version = 1, exportSchema = false)
@TypeConverters(Converter::class)
abstract class ToDoDatabase : RoomDatabase() {
abstract fun toDoDao(): ToDoDao
// companion object {
// @Volatile
// private var INSTANCE: ToDoDatabase? = null
//
// fun getDatabase(context: Context): ToDoDatabase {
// val tempInstance = INSTANCE
// if (tempInstance != null) {
// return tempInstance
// }
// synchronized(this) {
// val instance = Room.databaseBuilder(
// context.applicationContext,
// ToDoDatabase::class.java,
// "todo_database"
// ).build()
// INSTANCE = instance
// return instance
// }
// }
// }
companion object {
@Volatile
private var INSTANCE: ToDoDatabase? = null
fun getDatabase(context: Context): ToDoDatabase =
INSTANCE ?: synchronized(this) {
INSTANCE
?: buildDatabase(context).also { INSTANCE = it }
}
private fun buildDatabase(context: Context) =
Room.databaseBuilder(
context.applicationContext,
ToDoDatabase::class.java, "todo_database"
).build()
}
}