In this lesson, we'll describe the
@Query annotation and see how it can be used to read data from the Room database.
We'l see how Room's compile-time query checking works to ensure that the SQL you write is valid from the moment you write it.
We demonstrate how to write a function in the
DAO which returns all of the Tasks currently in the database using the
For this lesson, we manually call the
DAO to obtain tasks from the
onCreate method and after we've inserted a Task into the DB. This pattern of manually calling the refresh function isn't ideal and we'll go on to highlight a better way of doing this using
LiveData in another lesson. But for now, this will teach the basics of how to invoke a query defined using the
Summary of Content:
- Room provides compile-time query checking for
- Room provides syntax highlighting on the query
- Not just checking syntax; Room actually checks the query against the database schema you have defined
- Room also checks the return value specified to ensure what is being returned from the SQL query can be mapped to the specified return type
- How to read the DB from a background thread but update the UI from the main thread