The Database class ties entities and DAOs together:
@Database(entities = [NoteEntity::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun noteDao(): NoteDao
companion object {
@Volatile private var INSTANCE: AppDatabase? = null
fun getInstance(context: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"app_database"
).build().also { INSTANCE = it }
}
}
}
}
This is the classic thread-safe singleton pattern: @Volatile ensures visibility of the field across threads, and the synchronized block prevents two threads from both creating a database instance simultaneously. You only ever want one AppDatabase instance per app process.
When your schema changes later (adding a column, a new table), you must bump version and supply a Migration — Room will crash on launch if it detects a schema mismatch without one, by design, to prevent silent data corruption.
- Image
- SKU
- Rating
- Price
- Stock
- Availability
- Add to cart
- Description
- Content
- Weight
- Dimensions
- Additional information