Android architecture component room Guide

1、 Introduction

Room is a data persistence component library in the Android architecture component library launched by Google. It can also be said to be a set of ORM solutions implemented on SQLite.

Room mainly consists of three parts:

The relationship is shown in the figure below:

Room Architecture Diagram

2、 Basic use

1. Create entity

1.1 a simple entity

A simple entity is defined as follows:

1.2 relationship between entities

Unlike most existing ORM libraries, room does not support direct references between entity objects.

However, the room allows the relationship between entities to be represented by foreign keys.

As shown in the above code, the book object and the user object belong to. User in book_ ID, corresponding to the ID in user. What happens to the corresponding book when a user object is deleted?

@There are two properties ondelete and onupdate in the ForeignKey annotation. These two properties correspond to ondelete() and onupdate() in ForeignKey. The values of these two properties are used to set the response of the book object when the user object is deleted / updated. The optional values of these two properties are as follows:

1.3 object nesting

In some cases, data in a table will be represented by multiple POJO classes. In this case, nested objects can be annotated with @ embedded, such as:

In the user table generated by the above code, the column is ID, firstname, street, state, city and post_ code

2. Create a data access object (DAO)

Dao can be an interface or an abstract class. Room will create the implementation of Dao at compile time.

Tips:

The definitions of addition, deletion and modification methods in Dao are relatively simple. We won't discuss them here. Let's talk more about query methods.

2.1 simple query

Talk is heap, show code directly:

Room will verify the SQL statement during compilation. If the SQL statement in @ query () has syntax errors or the queried table does not exist, room will report errors during compilation.

2.2 query parameter transfer

The code should be easy to understand. Pass the parameter Arg in the method and use: Arg in the SQL statement. During compilation, room will match the corresponding parameters.

If the parameter corresponding to arg is not matched in the parameter passed, room will report an error during compilation.

2.3 query the information of some fields in the table

In an actual business scenario, we may only care about the values of some fields in a table. At this time, I only need to query the concerned columns.

POJO classes that define subsets:

To add a query method to a Dao:

The POJO defined here also supports @ embedded

2.3 return type of query result

In addition to returning POJO objects and their lists, the query operation in room also supports:

Livedata < T >: livedata is another component provided in the architecture component library, which can well meet the needs of data change driven UI refresh. Room will implement the code to update livedata.

Cursor:

The purpose of returning cursor is to support the scenario of using cursor in existing projects. The official does not recommend returning cursor directly

Caution: It's highly discouraged to work with the Cursor API because it doesn't guarantee whether the rows exist or what values the rows contain. Use this functionality only if you already have code that expects a cursor and that you can't refactor easily.

2.4 associated table query

Room supports associated table query. The interface definition is not different from other queries, mainly SQL statements.

3. Create database

The database in the room is similar to the sqliteopenhelper in the SQLite API. It is the entry point for providing DB operations. However, in addition to holding the DB, it is also responsible for holding the data access object (DAO) of the relevant data table (entity). Therefore, defining the database in the room needs to meet three conditions:

After creating the three components of the above room, you can create a database instance in the code through the following code.

3、 Database migration

3.1 room database upgrade

In the traditional SQLite API, if we want to upgrade the database, we usually execute the SQL statements of database upgrade in the sqliteopenhelper.onupgrade method. The SQL statements are usually managed in the form of files or arrays according to the database version. Some people say that upgrading the database in this way is like dismantling the bomb. In contrast, upgrading the database in room is like pressing a switch.

Room provides the migration class to upgrade the database:

When creating the migration class, you need to specify startversion and endversion, and the migration in the code_ 1_ 2 and migration_ 2_ The startversion and endversion of version 3 are incremented. In fact, migration supports the direct upgrade from version 1 to version 3, as long as the statements executed in its migrate () method are normal. So how does room upgrade the database? In fact, it essentially calls sqliteopenhelper.onupgrade. Room implements a sqliteopenhelper. Migration is triggered when the onupgrade () method is called. When accessing the database for the first time, room does the following:

From this point of view, processing database upgrade in room is really like adding a switch.

3.2 migrate the original SQLite database to room

Because the room also uses SQLite, it can well support the migration of the original SQLite database to the room.

Suppose that the original database with version number 1 has a table user. To migrate to room, we need to define entity, Dao and database, and then add an empty migration when creating the database. It should be noted that the version needs to be upgraded even if there is no upgrade operation on the database, otherwise the exception IllegalStateException will be thrown

4、 Complex data processing

In some scenarios, our application may need to store complex data types, such as date, but the entity of room only supports the conversion between the basic data type and its packing class, and does not support other object references. Therefore, room provides TypeConverter for users to implement corresponding conversion.

The conversion of a date type is as follows:

After defining the conversion method, you can specify it on the corresponding database, so that you can use the date class in the corresponding POJO (user).

5、 Summary

In the project of realizing data persistence through SQLite API, I believe there is a sqliteopenhelper implementation with heavy tasks, a pile of constant classes for maintaining table fields, and a pile of database access classes (DAO) with similar code. When accessing the database, you need to traverse the cursor, construct and return the corresponding POJO class... In contrast, As an ORM library encapsulated on SQLite, room does have many advantages. The more intuitive experience is:

summary

The above is the Android architecture component room guide introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>