Android Jetpack: Room


at Google i/o 2017 we opened the door to room a persistence library that provides an abstraction layer over SQLite now room has rich version 2.

0 and is part of jetpack we fix the bugs you reported and added some of the features you asked for let's go over rooms main components and see what queries are supported how to implement my gracious and how to test your work with a database let's say that you want to have a table of users and you want every row of that table to be an instance of the user class annotate your class with that entity define the table name if you don't want to use the name of the class a stable name set the mandatory primary key and the optional column info but only if you want to change the name of the color otherwise the field name is used and that's it we'll take care of the creation of the user table for you so that's how the entity is done but we need a way to access the data in the database we do that with data access objects dolls for short or precisely create an interface annotated with at TAO in this interface declare all the methods needed to work with the database annotating them with the corresponding SQL query room takes care of implementing these methods for you the supported queries are insert update delete query and Rock query all of them except row query are checked at compile time which means that if you write an invalid query you'll find this out immediately the closet puts together the entities and the dolls is the room database create an abstract class that extends the room database annotated declare the entities and the corresponding dolls let's take a closer look at the queries the return type of query and raw query can be the entire entity but also a subset of its fields if you're working with guava or with optional from the java.

util package you can also use them as return types so this means that if there are no values to satisfy your query then your query will return optional dot empty or optional dot absent depending on which optional you're working with all of these queries are synchronous meaning that they will be run on the same thread you are triggering them from room ensures you follow best practices by throwing an error if you run queries on the main thread so use your preferred method of handling threads in Android and make sure you're off the main thread room also supports asynchronous query when working with live data or chav what's more the cars that return life data or flowable are observable queries meaning that you will get notified every time the data in the table or tables updates whenever you alter your database schema either because you've added or renamed a column or table you need to tell the database how to handle that change in order to do that in your database class you'll need to first update your database version second implement a migration class which defines how to handle the migration from the old schema to the new one and then thirdly add that migration class as a parameter to the database filter after triggering the migrations validates the schema for you to ensure that the migration was done correctly if you don't want to handle migrations and you don't need to preserve your database data call fall back to destructive migrations when building the database to destructively recreate the database only from a specific version on-call fall back to destructive migration from then provide the number for that version so we have our entities downs database and migrations how do we test them to test the dolls you'll need to implement an android a unit test that creates an in-memory database the in-memory database holds the data only for as long as the process is alive meaning that after every test the database is destroyed to test asynchronous queries use an instant task executor role to execute each task synchronously in our apps implementation you'll end up referencing the dowels in other classes to unit test those classes just smoke the down or implement the fake version here's another tip to implement espresso tests covering code that uses asynchronous queries extend the counting task executor role to count the tasks as they start and finish finally don't forget to test the migrations export the database schema first and then use another hand apestrong the migration test helper this class allows you to create the database in an older version and run and validate each migration all you need to do is check that the data you inserted in the other version is also present after the migration okay so let's summarize this let's swallow plate code compile time check queries ease of implementing migrations idea of testability and checks for keeping the database worked away from the main threat all of these qualities of room make it easier and more pleasant to work with databases helping you deliver better apps [Applause] you.

Leave A Reply

Your email address will not be published.