Jon Rumsey

An online markdown blog and knowledge repository.


Project maintained by nojronatron Hosted on GitHub Pages — Theme by mattgraham

Saving, Defining, Relationships, and Accessing Data in Room

References

Saving Data in Room

Define Data Entities in Room

Define Data Relationships in Room

Accessing Data Using Room DAOs

Save Data In Local DB Using Room

Use Room to locally store non-trivial data for e.g. caching when device is offline.

Room overlays a SQLlite Instance and interfaces.

SQL Queries are verified at compile-time.

Annotations minimize boilerplate code.

DBs can be easily migrated.

Implement Room

Update build.grade to support RXJava, Guava, Test Helpers, and Paging3:

dependencies {
    def room_version = "2.4.2"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // optional - RxJava2 support for Room
    implementation "androidx.room:room-rxjava2:$room_version"

    // optional - RxJava3 support for Room
    implementation "androidx.room:room-rxjava3:$room_version"

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation "androidx.room:room-guava:$room_version"

    // optional - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

    // optional - Paging 3 Integration
    implementation "androidx.room:room-paging:2.5.0-alpha02"
}

Above code sample smipped from [Saving Data in Room].

Primary Room Components

Room Database. Can talk to rest of app and will call getDAO.

Data Access Objects (DAO): Get Entities from db and persist changes to DB.

Entities: Models stored in the DB as Tablular data records. Must support GET/SETters.

Android App: Common communication point between Room DB, DAO, and Entities.

Entities

Annotations: @Entity, @PrimaryKey, @ColumnInfo etc.

Entity parameters: e.g. (tableName = "users")

Key parameters:

Other Annotations

Full-text search @Fts3 (small disk volumes) and @Fts4. Args (launguageId='lang_id').

Specific Columns can be indexed if SDK Versions don't support FTS3 or FTS4, using an 'indices' property or the @Entity() annotation, then define the @Index annotations to specify the columns.

@AutoValue: Support Java Immutable Value Classes between DBs where a column contains identical data.

Relationships in Room

Room does not allow entities to directly reference each other for technical reasons.

Use an Intermediate Data Class:

[Example lifted from Define Data Relationships In Room]

Use the MultiMap approach:

[Example lifted from Define Data Relationships In Room]

DAO

Annotations: @Dao, @Query("select * from tbl_name") etc, @Insert, and @Delete.

Define Data Access Objects as an Interface or an Abstract Class, using @Dao annotations.

DAO Annotations

Annotations are used to define the operation the abstract DAO Method represents:

DAO Abstract Methods

Scaler abstract methods do NOT allow writing custom SQL statements.

Query abstract methods allow parameters defining custom SQL code.

Scalar abstract methods utilize a param 'onConflict' to resolve how to deal with a conflict on Write/Update.

Update Method: Attempts to update records in the database using an Entity instance.

Delete Method: Attempts to delete specific records in the DB using an Entity instance.

Query Method: Supports Simple SQL, JOIN methods, specific 'where' requirement queries, and MAP statements.

Paginated queries are allowed.

There are more details that should be examined before attempting to implement here

Database

A Database Class must be defined to represent the Room DB using annotation @Database.

Database Class must inclue an entities array listing data entities associated with the DB.

Database Class must be abstract and must extend RoomDatabase.

Database Class must define abstract methods w/ zero args returning instance of DAO class for each DAO Class that is associated with the DB.

Using the Database

Set up an instance of the database like so:

AppDatabase db = Room.databaseBuilder(getApplicationContext(),
  AppDatabse.class, 'db_name').build();

Above code sample smipped from [Saving Data in Room].

AppDatabase abstract methods can be used to get an instance of the DAO, e.g.:

UserDao userDao = db.userDao();
List<User> users = userDao.getAll();

Above code sample smipped from [Saving Data in Room].

Resources

Sample app "Sunflower" using Room

TV Tracking App using Room

CodeLab

7 Pro-tips for Room

Return to Root Readme