Thuta Learning
AdvancedMobile Developmentintermediate

Local Storage (Room Database)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Local Storage (Room Database) without the intimidation
  • Get comfortable running Android Studio/Compose code yourself
  • Apply this concept in a real project right away

Let's think about it this way for a second

Room is a library that wraps Android's built-in SQLite database in a type-safe, Kotlin-friendly way — there are three core concepts: Entity (`@Entity`, defines the table structure as a data class), DAO (`@Dao`, defines database operations - insert/query/delete - as an interface), and Database (`@Database`, the class that combines your Entities/DAOs). Pair Room with a ViewModel (Advanced lesson 1), and your data survives even an app restart — unlike RAM-only ViewModel state.

Let's connect this to a real scenario

Define `@Entity data class TodoEntity(@PrimaryKey val id: String, val title: String, val isDone: Boolean)`, then write `@Dao interface TodoDao { @Query("SELECT * FROM TodoEntity") suspend fun getAll(): List<TodoEntity>; @Insert suspend fun insert(todo: TodoEntity) }` — calling `todoDao.insert(newTodo)` from your ViewModel permanently saves the data to the device disk, so it's still there even if you force-close and reopen the app.

Let's walk through it together

kotlin
@Entity
data class TodoEntity(
    @PrimaryKey val id: String,
    val title: String,
    val isDone: Boolean
)

@Dao
interface TodoDao {
    @Query("SELECT * FROM TodoEntity")
    suspend fun getAll(): List<TodoEntity>

    @Insert
    suspend fun insert(todo: TodoEntity)
}

@Database(entities = [TodoEntity::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun todoDao(): TodoDao
}
You should see
Even after force-closing and reopening the app, you should see the todo list data still there (pulled back from Room).

5-minute try-it

Write `TodoEntity`, `TodoDao`, and `AppDatabase` yourself, then rewrite `TodoViewModel` (Advanced lesson 1) to call the Room DAO instead of using an in-memory list.

A quick word of caution

Using `fallbackToDestructiveMigration()` in a production app instead of a proper `Migration` strategy when the database schema changes can wipe out all of an existing user's data after an update.

Easy traps

  • Updating the database schema (an Entity field) but forgetting to bump the `version` number — this can crash existing users' apps after an update
  • Running a Room database operation directly on the main thread — calling it outside a `suspend` function can freeze the app

Now try it yourself

Write `TodoEntity`, `TodoDao`, and `AppDatabase` yourself, then rewrite `TodoViewModel` (Advanced lesson 1) to call the Room DAO instead of using an in-memory list.

You'll know it worked when: Even after force-closing and reopening the app, you should see the todo list data still there (pulled back from Room).

Local Storage (Room Database) | Thuta Learning