Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data Persistence With Room

Data Persistence With Room

Data persistence is a crucial aspect of Modern Android Development that ensures relevant user data is preserved providing a seamless user experience across sessions. This session slides, explore a Jetpack data persistence library: Room, for persisting data locally for offline access. We will cover how to set up and work with Room by defining its components, performing operations like insertion, updating and deleting data, migrations and inspecting your database.

Annunziata Kinya

December 04, 2023
Tweet

More Decks by Annunziata Kinya

Other Decks in Technology

Transcript

  1. What We Will Cover • Intro to Data Persistence •

    Room Library • Room Components • Performing CRUD operations with Room • Managing Migrations • Testing Migrations • Sample project demo
  2. Data Persistence • Storage of user-generated data to ensure longevity

    after an application/process that created it has been closed • Options for data persistence: ◦ Remote Storage - server ◦ Local storage - offline access ▪ DataStore ▪ Room Library ▪ (Realm?) Android App
  3. Room Library • Part of Android Jetpack Libraries • It's

    a wrapper that provides an abstraction layer over SQLite (relational database) • Data is represented in the form of tables where each column represent properties and each row represents actual records of the data • There should be a column that serves as the primary key and must be unique to each row in the database id Name Completed 1 Cook dinner false 2 Do laundry true
  4. Room Components • Main components for Room setup: ◦ Data

    entities ◦ Data access objects (DAO) ◦ Database class
  5. Entity • Represent tables in your app's database • Provides

    ways to customize e.g. ◦ tableName ◦ @Column Info • Other key annotations ◦ @Ignore ◦ @Embedded
  6. Data Access Object(DAO) • Provide methods that the app uses

    to perform operations e.g. insert data, delete etc • Convenience methods - no SQL statement ◦ @Insert ◦ @Update ◦ @Delete • Query methods - with SQL statement ◦ @Query ▪ To fetch data ▪ Return a subset of tables columns ▪ Pass a parameter to query ▪ Etc • OnConflictStategy - IGNORE, REPLACE, FAIL etc
  7. Database Class • Provides the database and serves as the

    main access point for accessing the persistent data • Defines the list of entities • Provides the app with instances of the DAOs associated with that database.
  8. Performing CRUD Operations • Call the DAO methods from your

    repository for the different operations as needed Flow View
  9. Migrations • As you add and change features in your

    app, you need to modify your Room entity classes and underlying database tables to reflect these changes. • This schema changes require migrations to preserve user data existing in the previous versions • Options for Incremental migration: ◦ Automated Migration ◦ Manual Migration
  10. Automated Migrations • Available from version 2.4.0-alpha01 and above •

    For basic schema changes e.g. adding a new column to a table • Relies on the generated database schema for both the old and the new versions of the database hence exportSchema should be set to true • Ambiguous schema changes that Room is not able to migrate will need an AutoMigrationSpec to give Room more input on the migration
  11. Manual Migrations • For more complex schema changes e.g. split

    the data in a table into two tables, added new table • Uses raw SQL statements • Explicitly define a migration class
  12. Testing Migrations • Use instrumented tests than run on your

    emulator • Test migrations across each version
  13. Resources • Android Basics With Compose Unit 6: Data persistence

    • Android Documentation: Save data in a local database using Room • Android Documentation: Migrate your Room database • Sample demo app for the session