0% found this document useful (0 votes)
43 views3 pages

Advantages of Room Persistence Library

The Room Persistence Library simplifies database operations by providing an abstraction layer over SQLite, ensuring compile-time verification of SQL queries and reducing boilerplate code. It consists of three components: the Database class, which serves as the main access point; Data Access Objects (DAOs) for defining database operations; and Data Entities representing tables. Additionally, cloud storage is advantageous for applications requiring multi-device synchronization, offering accessibility, data backup, scalability, and real-time sync.

Uploaded by

Adib Rusyaidi
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)
43 views3 pages

Advantages of Room Persistence Library

The Room Persistence Library simplifies database operations by providing an abstraction layer over SQLite, ensuring compile-time verification of SQL queries and reducing boilerplate code. It consists of three components: the Database class, which serves as the main access point; Data Access Objects (DAOs) for defining database operations; and Data Entities representing tables. Additionally, cloud storage is advantageous for applications requiring multi-device synchronization, offering accessibility, data backup, scalability, and real-time sync.

Uploaded by

Adib Rusyaidi
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

1.

​ What are the advantages of using Room Persistence Library to communicate with local
databases over using SQLite API Packages directly?

The Room Persistence Library offers several advantages over using the SQLite API directly:

Simplified Database Operations: Room provides an abstraction layer over SQLite, reducing the
need for boilerplate code and making database operations easier to implement.

Compile-Time Verification: Room validates SQL queries at compile time, reducing the risk of
runtime errors.

Integration with LiveData and RxJava: Room seamlessly integrates with LiveData and RxJava,
enabling reactive programming and automatic UI updates when data changes.

Type-Safe Queries: Room uses annotations to generate SQL queries, ensuring type safety and
reducing errors.

Reduced Boilerplate Code: Room automates tasks like creating tables and managing database
connections, reducing the amount of manual coding required.

Improved Maintainability: Room’s structured approach (entities, DAOs, and database classes)
makes the codebase more organized and easier to maintain.

2.​ Briefly explain the three components in Room Database.

a) ​ Database class

The Database class is an abstract class that extends RoomDatabase. It serves as the main
access point to the underlying SQLite database.

It is annotated with @Database and lists the entities (tables) and the database version.

It provides methods to access the Data Access Objects (DAOs) that interact with the
database.

b) ​ Data Access Object

The DAO is an interface or abstract class that defines the methods to access the database.

It is annotated with @Dao and contains methods for inserting, updating, deleting, and
querying data.

DAOs use SQL queries (annotated with @Query) or convenience methods (e.g., @Insert,
@Update, @Delete) to perform database operations.
c) ​ Data Entity

The Data Entity represents a table in the database. It is a class annotated with @Entity.

Each field in the class corresponds to a column in the table, and the class itself represents
a row.

The primary key is defined using the @PrimaryKey annotation, and relationships
between tables can be defined using annotations like @ForeignKey.

3.​ Identify the differences between the Query Method and the Convenience Method in Data
Access Objects

Aspect Query Method Convenience Method

Definition Custom SQL queries written by the Predefined methods provided by


developer using the @Query Room (e.g., @Insert, @Update,
annotation. @Delete).

Flexibility Highly flexible, allowing complex Limited to basic CRUD operations


queries (e.g., joins, filtering, sorting). (insert, update, delete).

Usage Used for custom operations that Used for simple, common database
cannot be handled by convenience operations.
methods.

Example @Query("SELECT * FROM @Insert void


user WHERE age > insertUser(User user);
:minAge")

Performance Can be optimized for specific use Automatically optimized by Room


cases but requires manual SQL for basic operations.
writing.
4.​ Other than storing data in local storage (including internal and external local storage), we
can also use cloud storage to store our application data. Provide one example of a
scenario where cloud storage is a better option than local storage.

Scenario: Multi-Device Synchronization

Explanation: Cloud storage is a better option when an application needs to synchronize data
across multiple devices. For example, a note-taking app like Google Keep or Evernote allows
users to access their notes from any device (phone, tablet, or computer). Storing data in the
cloud ensures that changes made on one device are automatically reflected on all other
devices.

Advantages:

Accessibility: Users can access their data from anywhere with an internet connection.

Data Backup: Cloud storage provides automatic backup, reducing the risk of data loss.

Scalability: Cloud storage can handle large amounts of data and scale as the user base grows.

Real-Time Sync: Changes are synchronized in real-time across all devices.

You might also like