Detailed explanation of the use of Android architecture component room

Room is actually an ORM that abstracts the use of SQLite, but it is the son orm of Android and supports the nested use of livedata and rxjava. It's good to learn.

Room has three main components

Simple use

Add Google Maven warehouse

Add dependency

Define data table entity classes

Class table

Student list

Entity annotation optional parameters

Index annotation optional parameters

ForeignKey foreign key annotation optional parameters

Define Dao class

@Insert and @ update can execute transaction operations, which are defined in onconflictstrategy annotation class

Define database

Generate database instance

Other operations for generating database instances

Database migration (upgrade)

Create nested objects

Sometimes you want to represent an entity or an ordinary previous Java object (POJO) as a complete whole in the database logic, even if the object contains several fields. In these cases, you can use @ embedded to represent an object that you want to decompose into child fields in the table. You can then query the embedded fields as you would any other single column

In this way, the fields in the user table contain ID, firstname, street, state, city, and post_ code

Note: embedded fields can also contain other embedded fields

If an entity has multiple embedded fields of the same type, you can make each column unique by setting the prefix attribute (prefix). The supplied value is then added to the beginning of each column name in the embedded object

Use with livedata

Add dependency

Modify return type

Use with rxjava

Add dependency

Modify return type

Direct cursor access

Type conversion

Define the transformation class, and the @ TypeConverter annotation defines the transformation method

@The typeconverters annotation tells the database which conversion classes to rely on

Using these converters, you can use your custom types in other queries, just as you will use basic types, as shown in the following code

Output mode

When compiling, export the schema information of the database to the JSON file, which can help us better debug and debug

You should store the exported JSON file (representing the schema history of the database) in your version control system because it allows you to create older versions of your database for testing purposes

summary

The above is a detailed explanation of the use of the Android architecture component room 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
分享
二维码
< <上一篇
下一篇>>