0% found this document useful (0 votes)
18 views

Droid - Database - Room

Uploaded by

mauahezekia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Droid - Database - Room

Uploaded by

mauahezekia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Local Database - Room

AVP with Android


Room
• Apps that handle non-trivial amounts of structured data can benefit greatly from persisting that data locally.

• The most common use case is to cache relevant pieces of data so that when the device cannot access the network, the user can still browse
that content while they are offline.

• The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of
SQLite.

• In particular, Room provides the following benefits:

1 Compile-time verification of SQL queries.

2 Convenience annotations that minimize repetitive and error-prone boilerplate code.

3 Streamlined database migration paths.


Primary components
There are three major components in Room:

1 The database class that holds the database and serves as the main
access point for the underlying connection to your app's persisted
data.

2 Data entities that represent tables in your app's database.

3 Data access objects (DAOs) that provide methods that your app
can use to query, update, insert, and delete data in the database.
Data entity
The following code defines a Student data entity.

Each instance of Student represents a row in a user table in the app's database.
Data access object (DAO)
The following code defines a DAO called StudentDao.

StudentDao provides the methods that the rest of the app uses to interact with
data in the user table.
Database
The following code defines an StudentDatabase class to hold the database.

StudentDatabase defines the database configuration and serves as the app's


main access point to the persisted data.

The database class must satisfy the following conditions:

1 The class must be annotated with a @Database annotation that includes an entities
array that lists all of the data entities associated with the database.

2 The class must be an abstract class that extends RoomDatabase.

3 For each DAO class that is associated with the database, the database class must define
an abstract method that has zero arguments and returns an instance of the DAO
class..
Usage
After you have defined the data entity, the DAO, and the database object, you can use the following code to create an
instance of the database:

You can then use the abstract methods from the StudentDatabase to get an instance of the DAO.

In turn, you can use the methods from the DAO instance to interact with the database:
https://developer.android.com/training/data-storage/room

https://developer.android.com/training/data-storage/room/accessing-data

You might also like