What Else Does Room Offer?
Room has a wide array of other capabilities, besides the simple CRUD (create-read-update-delete) operations seen in these to-do classes, such as:
- You can add properties to the
@Entityannotation to rename the table (tableName), define indexes (indices), and so on -
@Query-annotated functions that have parameters can inject those parameters into the SQL:
@Query("SELECT * FROM todos WHERE id = :modelId")
fun find(modelId: String): LiveData<ToDoEntity>
-
@Insert,@Update, and@Deletefunctions can accept a single entity, aListof entities, or a variable argument list of entities (e.g.,varargin Kotlin) - Using
@TypeConverterand@TypeConvertersannotations, you can teach Room how to save arbitrary data types into a single column, such as converting aCalendarorLocalDateinto aLongorstring - Room supports
@ForeignKeyannotations to describe relations between two entity classes… but the entities cannot directly refer to the other entities via properties or fields. Room requires you to retrieve those related objects separately and tie them together on your own. - When you do update your database schema by adding, changing, or removing entities, you may need to run some code to convert the older database contents to the new schema when the user upgrades your app. Room has a
Migrationinterface and related code to help you set up those conversions. - In addition to
LiveDataand Kotlin coroutines, Room also has add-on libraries that add support for RxJava (e.g., queries returning anObservableorSingle). - By default, each SQL statement runs in its own transaction. However, through things like the
@Transactionannotation, you can define your own custom transaction boundaries, where you have several SQL operations that should succeed or fail as a whole. - Room, in conjunction with the Jetpack Paging library, supports progressive loading of large data sets, typically based on a user scrolling through a list, to minimize how much memory is needed at any one point in time.
- Room supports a pluggable database implementation. It defaults to the standard copy of SQLite that is available to any Android app, but you can replace that with some other implementation, such as SQLCipher for Android for an encrypted database.
- Room has support for SQLite’s full-text search (FTS) engine and other SQL constructs, such as views.
Note that Elements of Android Room covers Room in significantly more depth.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.