Room: provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite

Copied from https://codelabs.developers.google.com/codelabs/build-app-with-arch-components/index.html#4

为什么要缓存数据? Why Cache Weather Data?

Download the latest weather data from the server every time is incredibly inefficient, and if the user is offline, there's no way they can use your app.

为什么寻求替代 SQLite 的方案?

SQLite includes lots of boilerplate code, and no easy way to validate your SQLite statements during compile time.

Room 是什么?代替 SQLite 有哪些优点?

Room is SQLite object mapping library. Less boilerplate code, Compile-time validation of SQL queries, Allows for data observation via LiveData.

Components of Room

  • 数据表 @Entity - This component defines the schema of a database table. Model objects can easily be converted to Entity objects.

  • 数据库 @Database - This component represents a database holder. In this class you define a list of entities for the database and the data access objects (DAOs) for the database. Then you use this class to create a new database or acquire a connection to a database during runtime.

  • 数据库访问接口 @DAO - This component represents a class or interface as a Data Access Object (DAO). DAOs are responsible for defining the methods that access the database. They provide an API for reading and writing database data.