Overview of Room Persistence Library

Learn about Room database components, its architecture, and why we need it.

Introduction

Android apps can utilize SQLite databases to persist structured data. SQLite is a cross-platform SQL-based database that’s stored as a single file on the disk. The Room persistence library is a wrapper on top of the SQLite database layer. It’s part of Android Jetpack Components, and it simplifies database access by providing an easy-to-use abstraction layer on top of SQLite.

Some of the features provided by Room are as follows:

  • It provides annotations for common database operations, eliminating the need to write boilerplate code.

  • It handles database migration by exposing automatic and custom migration workflow methods.

  • It reduces debugging effort by verifying all SQL queries at compile-time.

  • It provides seamless integration with lifecycle-aware components, such as Live Data and ViewModel.

Components

The Room persistence library provides three major components:

  • Entity : This represents individual tables in the SQLite database.

  • Dao : This provides methods for database CRUD (Create, Read, Update, Delete) operations.

  • Database : This lets us define the database configuration along with its migration paths.

Architecture

The diagram below shows the Room database's architecture and other Android components. The bottom layer comprises Room database components(Entity, Dao, and Database). These components abstract all direct interactions with the underlying SQLite layer.

Get hands-on with 1200+ tech skills courses.