https://developer.android.com/codelabs/basic-android-kotlin-compose-persisting-data-room
Gitlab repo: https://github.com/google-developer-training/basic-android-kotlin-compose-training-inventory-app/tree/main
Room is a persistence library that's part of Android Jetpack. Room is an abstraction layer on top of a SQLite database. SQLite uses a specialized language (SQL) to perform database operations. Instead of using SQLite directly, Room simplifies the chores of database setup, configuration, and interactions with the app. Room also provides compile-time checks of SQLite statements.
…
…
You can browse the code in the Inventory app GitHub repository.
@Entity annotation marks a class as a database Entity class.data.@Entity annotation has several possible arguments. By default (no arguments to @Entity), the table name is the same as the class name. Use the tableName argument to customize the table name.@PrimaryKey to make the id the primary key.0, which is necessary to auto generate values.autoGenerate parameter to the @PrimaryKey annotation to specify whether the primary key column should be auto-generated. If autoGenerate is set to true, Room will automatically generate a unique value for the primary key column when a new entity instance is inserted into the database.@Insert@Delete @Update@QueryonConflict tells the Room what to do in case of a conflict. The OnConflictStrategy.IGNORE strategy ignores a new item.Flow in the persistence layer. With Flow as the return type, you receive notification whenever the data in the database changes. The Room keeps this Flow updated for you, which means you only need to explicitly get the data once.Flow return type, Room also runs the query on the background thread. You don't need to explicitly make it a suspend function and call it inside a coroutine scope.RoomDatabase class and annotate it with @Database. This class has one method that returns the existing instance of the RoomDatabase if the database doesn't exist.public abstract class that extends RoomDatabase. The new abstract class you define acts as a database holder. The class you define is abstract because Room creates the implementation for you.@Database. In the arguments, list the entities for the database and set the version number. The @Database annotation requires several arguments so that Room can build the database.exportSchema to false so as not to keep schema version history backups.companion object, which allows access to the methods to create or get the database and uses the class name as the qualifier.companion object, declare a private nullable variable Instance for the database and initialize it to null.Instance with @Volatile. Instance, while still inside the companion object, define a getDatabase() method with a Context parameter that the database builder needs.“Template code” for future projects:
import android.content.Context import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase /** * Database class with a singleton Instance object. */ @Database(entities = [Item::class], version = 1, exportSchema = false) abstract class InventoryDatabase : RoomDatabase() { abstract fun itemDao(): ItemDao companion object { @Volatile private var Instance: InventoryDatabase? = null fun getDatabase(context: Context): InventoryDatabase { // if the Instance is not null, return it, otherwise create a new database instance. return Instance ?: synchronized(this) { Room.databaseBuilder(context, InventoryDatabase::class.java, "item_database") .build() .also { Instance = it } } } } }
…
…