persist_data_with_room
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| persist_data_with_room [2026/04/01 09:44] – [3. Starter app overview] kalaschnikow | persist_data_with_room [2026/04/01 15:31] (current) – [Persist Data with Room] kalaschnikow | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Persist Data with Room ====== | ||
| + | |||
| + | https:// | ||
| + | |||
| + | Gitlab repo: https:// | ||
| + | ===== 1. Before you begin ===== | ||
| + | |||
| + | [[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, | ||
| + | |||
| + | ... | ||
| + | ===== 2. App overview ===== | ||
| + | |||
| + | ... | ||
| + | ===== 3. Starter app overview ===== | ||
| + | |||
| + | You can browse the code in the [[https:// | ||
| + | |||
| + | * ui/ | ||
| + | * list of items | ||
| + | * ui/ | ||
| + | * item details | ||
| + | * ui/ | ||
| + | * navigation graph for the entire application | ||
| + | ===== 4. Main components of Room ===== | ||
| + | * Room entities represent tables in your app's database. You use them to update the data stored in rows in tables and to create new rows for insertion. | ||
| + | * Room DAOs provide methods that your app uses to retrieve, update, insert, and delete data in the database. | ||
| + | * Room Database class is the database class that provides your app with instances of the DAOs associated with that database. | ||
| + | ===== 5. Create an item Entity ===== | ||
| + | |||
| + | * An Entity class defines a table, and each instance of this class represents a row in the database table. | ||
| + | * The '' | ||
| + | * Each field of the Entity is represented as a column in the database, unless denoted otherwise. | ||
| + | |||
| + | * Data classes are primarily used to hold data in Kotlin. They are defined with the keyword '' | ||
| + | * The '' | ||
| + | |||
| + | * Annotate the id property with '' | ||
| + | * If you want to auto generate values | ||
| + | * Add the '' | ||
| + | ===== 6. Create the item DAO ===== | ||
| + | * The Data Access Object (DAO) is a pattern you can use to separate the persistence layer from the rest of the application by providing an abstract interface. This isolation follows the single-responsibility principle, which you have seen in previous codelabs. | ||
| + | * The DAO you create is a custom interface that provides convenience methods for querying/ | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * ...? | ||
| + | | ||
| + | * The argument '' | ||
| + | |||
| + | * It is recommended to use '' | ||
| + | * Because of the '' | ||
| + | ===== 7. Create a Database instance ===== | ||
| + | |||
| + | * The Database class provides your app with instances of the DAOs you define. | ||
| + | * You need to create an abstract '' | ||
| + | |||
| + | * Create a '' | ||
| + | * Annotate the class with '' | ||
| + | * Specify the list of entities. | ||
| + | * Set the version. Whenever you change the schema of the database table, you have to increase the version number. | ||
| + | * Set '' | ||
| + | * define a '' | ||
| + | * Inside the '' | ||
| + | * Annotate '' | ||
| + | * The value of a volatile variable is never cached, and all reads and writes are to and from the main memory. These features help ensure the value of Instance is always up to date and is the same for all execution threads. It means that changes made by one thread to Instance are immediately visible to all other threads. | ||
| + | * Below '' | ||
| + | * ... | ||
| + | |||
| + | |||
| + | " | ||
| + | |||
| + | <code kotlin> | ||
| + | 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:: | ||
| + | abstract class InventoryDatabase : RoomDatabase() { | ||
| + | |||
| + | abstract fun itemDao(): ItemDao | ||
| + | |||
| + | companion object { | ||
| + | @Volatile | ||
| + | private var Instance: InventoryDatabase? | ||
| + | |||
| + | fun getDatabase(context: | ||
| + | // if the Instance is not null, return it, otherwise create a new database instance. | ||
| + | return Instance ?: synchronized(this) { | ||
| + | Room.databaseBuilder(context, | ||
| + | .build() | ||
| + | .also { Instance = it } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== 8. Implement the Repository ===== | ||
| + | |||
| + | ... | ||
| + | |||
| + | |||
| + | ===== 9. Add the save functionality ===== | ||
| + | |||
| + | ... | ||
| + | ===== View the database content using Database Inspector ===== | ||
| + | |||
| + | ===== Get the solution code ===== | ||
| + | |||
| + | ===== Summary ===== | ||
| + | |||
| + | ===== Learn more ===== | ||
