Room Database
Discover how to implement Room database for offline support in your Travel Blog app. Learn to define entities, create DAOs, and build a database with Room annotations. This lesson guides you to efficiently store and manage app data locally while understanding best practices like singleton patterns.
We'll cover the following...
Dependencies #
To use the Room database, we need to add it to our dependencies list:
Because Room database heavily relies on custom annotations, we also added Room annotationProcessor dependency.
Entities #
Now, we need to tell Room what entities we want to save to the database. Let’s open the Blog class and add the @Entity annotation (1). Doing so, we tell the Room to create a table for the blog entity.
To define a primary key we can simply use @PrimaryKey annotation on the id field (2). While the Room library can automatically persist all Java primitives, it can’t persist custom objects, like Author. To save ...