An online markdown blog and knowledge repository.
Define Data Relationships in Room
Accessing Data Using Room DAOs
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.
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].
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.
Annotations: @Entity
, @PrimaryKey
, @ColumnInfo
etc.
Entity parameters: e.g. (tableName = "users")
Key parameters:
(primaryKeys = {"firstName", "lastName"})
uses combo of columns for a 'composite primary key'.@Ignore
. Use to not store the field data in the DB e.g. images etc.@Entity(ignoredColumns="col_name")
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.
Room does not allow entities to directly reference each other for technical reasons.
Use an Intermediate Data Class:
@Query()
annotation e.g. ("select user.name AS userName, book.name AS bookname from user, book where user.id = book.user_id)
.[Example lifted from Define Data Relationships In Room]
Use the MultiMap approach:
Map<T, List<U>>
.[Example lifted from Define Data Relationships In Room]
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.
Annotations are used to define the operation the abstract DAO Method represents:
@Insert
: Define a non-implemented method with return type void called 'insertAll' or similar that takes a Collection as method parameter.@Delete
: Define a non-implemented method with return type void called 'delete' or similar that take a single Instance as a method parameter.@Query(SQL_statement)
: Define a non-implemented method with a Collection return type called "getAll" or similar.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
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.
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].
Sample app "Sunflower" using Room
Return to Root Readme