Room Flashcards

1
Q

What is Room?

A

Room is a database library that’s part of Android Jetpack.

Under the hood, the Room library is an abstraction layer on top of a SQLite database.

Instead of using SQLite directly, Room simplifies the chores of setting up, configuring, and interacting with the database.

It also makes it possible for your app to interact with the database using ordinary function calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What needs to be added to a data class to make it a Room entity?

A
1. Annotate the class signature
@Entity(tableName = "my_table_name"
data class MyEntity(...)
  1. Annotate the id with a Primary Key
    @PrimaryKey(autoGenerate = true)
    var myId: Long = 0L
  2. Annotate remaining properties with
    @ColumnInfo(name = “my_var_name”)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does DAO stand for?

What is the DAO?

A

The Data Access Object can be thought of as a custom interface for accessing your database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which annotations are needed to create a DAO?

A

@Dao
interface MyDatabaseDao{ }

@Insert
suspend fun insert(myobject: MyEntity)

@Update
suspend fun update(myobject: MyEntity)

@Query (“SQL COMMAND WHERE myId = :key”) *
suspend fun getOrDeleteEntries(key: Long ): MyEntity? **

*when searching using a parameter (usually the id), use the function’s parameter name with a colon in front to place in the SQL query

**functions need nullable return Types when fetching information

function names do NOT have to match query annotations - they just do in this example for insert and update

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why use a query to clear a table instead of the Room provided @Delete annotation?

A

@Delete is a useful helper if you know which items need to be removed from the DB - supply it with a list of item id’s to delete.

It is not efficient for clearing all entries.
@Query(“DELETE FROM table_name”)
is cleaner.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

It’s a good idea to use which return type when creating a function to fetch all DB entries?

A

LiveData^List^MyEntity^^

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define the SQL queries for the following:

  1. fetch a single entry by id
  2. clear all table entries
  3. fetch the most recent DB entry
  4. fetch all entries
A

1.
(“SELECT * FROM table_name WHERE myID = :key”)

2.
(“DELETE FROM table_name”)

3.
(“SELECT * FROM table_name ORDER BY myId DESC LIMIT 1”)

  1. (“SELECT * FROM table_name ORDER BY myID DESC”)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The database class is mostly boilerplate, but what are the key things it does?

A
  1. @Database(entities = [MyEntity::class], version = 1, exportSchema = false)
2. abstract class MyDatabase : RoomDatabase() {
This class is abstract because it is a template for Room to provide the implementation.
  1. In a companion object, the function getInstance( ) returns a Singleton Database
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain what these do:

  1. exportSchema = false
  2. @Volatile
  3. synchronized(this){ }
  4. .fallbackToDestructiveMigration()
    .build()
A
  1. Schema version history backups do not need to be kept
  2. Annotated var is only read from main memory - it is not cached - so anything using its value always gets the up-to-date value
  3. Only one thread can access this block at one time - prevents multiple concurrent instantiations of the DB
  4. This is a “dummy” migration strategy (it simply destroys and rebuilds the DB) to avoid
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the syntax used to create a DB instance?

A

instance = Room.databaseBuilder(
context.applicationContext,
MyDatabase::class.java,
“my_database_name”
)
.fallbackToDestructiveMigration()
.build()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When using Room, why is it a good idea to have your ViewModel extend AndroidViewModel rather than ViewModel?

A

AndroidViewModel extends ViewModel and is aware of the Application context.

MyDatabase.getInstance( ) takes a context as its only parameter. This is called in Fragment.onCreateview( ).

In Fragment.onCreateView( ), we can create an application variable and pass it to both Database.getInstance( ) and the ViewModelFactory:

val application = requireNotNull(this.activity).application

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why do we not need to specify any Dispatchers when using Room?

A

Room handles Dispatchers for us.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is different about fetching DB entries to be returned as LiveData?

A

The @Query does not need to be specified as a suspend fun because Room automatically handles this on a background thread.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. Which dependencies need to be added to implement Room?
  2. Which dependency provides extensions and coroutine support for Room?
  3. And for test helpers?
A

1.
implementation “androidx.room:room-runtime:$room_version”

kapt “androidx.room:room-compiler:$room_version”

2.
implementation “androidx.room:room-ktx:$room_version”

3.
testImplementation “androidx.room:room-testing:$room_version”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. What Room feature makes handling LiveData easier?

2. How to handle LiveData holding String values to be displayed on the screen?

A
  1. A Room @Query returning a LiveData, assigned to a ViewModel variable, will update the variable’s value every time the DB changes.
  2. Following
    val myList = database.getEntries( )

Transform the list
val entriesString = Transformations.map(myList){ entriesList -^ // manipulate entries to String or list of Strings}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you overwrite a database entry if there is a key conflict when using @Insert ?

A

@Insert(onConflict = OnConflictStrategy.REPLACE)