Working with Room Databases

Learn how to create a Room database instance and handle migrations.

Introduction

Our Android app needs to define a database instance to access the data defined in the SQLite tables. In the previous lesson, we learned that the Room persistence library offers three major components to define entities, DAOs, and the database itself. The database class is the app’s entry point for interacting with the database.

Defining a Room database

The Database annotation can be used on an abstract class that extends the RoomDatabase to define a database instance. The database should define abstract methods for every DAO class that we want to associate with our database.

To refresh our memories, let’s take a look at UserDao, which we defined in the previous lesson. The UserDao defines methods to interact with the User table. We’ve modified the DAO methods to use Kotlin coroutines by adding the suspend keyword. Kotlin coroutines let us invoke our methods on a background thread.

Get hands-on with 1200+ tech skills courses.