In this tutorial we are going to talk about how we can implement in our Android applications the Room persistent library. For this, we gonna create a whole new app. So, you can have a big picture about this library. In the introduction part we are going to summarize the tutorial.
Previously, if we wanted to store data locally on an Android device, we had to create manually the database helpers and the SQL queries. It took a long time and it wasn’t an easy task, because we could make many errors also, what what we got only at runtime.
Then Room come to the rescue. It was released in the Google I/O 2016. Room is one of Google’s architecture component library for working with SQLite on Android.
With the release of Version 2.1, the library added support for database transactions using coroutines.
Apps that handle non-trivial amounts of structured data can benefit greatly from persisting that data locally. The most common use case is to cache relevant pieces of data. That way, when the device cannot access the network, the user can still browse that content while they are offline. Any user-initiated content changes are then synced to the server after the device is back online.
By using annotations, we can create our database, tables and also the queries. The Room library will automatically translate these information and annotations into SQLite queries.
Room has three important components
- Entity: It represents a table in the Room database, which should be annotated with the @Entity keyword.
- DAO: It stands for Data Access Object, which is an interface, that contains the queries to access the database. It is annotated with @DAO
- Database: It represents the database. Because it holds a connection to the SQLite database, all operations are executed through it. It is annotated with @Database.
In this tutorial, to show you how does Room works, we are going to create a whole new app. This application will contain a RecyclerView, a Floating Action Button and a DialogFragment, where we can create and modify the created items.
This application will be a ToDo list app. You can add a title and a description to your todo, and you can schedule it. Every todo item will have a priority as well, which can be low, medium or high.
In this sample application we are going to use some other Architecture Components as well, like
- Data Binding
- Navigation Component
One more thing, the application will be uploaded to GitHub. Every chapter will have its own branch.
So it is also important to know how you can use GitHub and Git in Android Studio.
I hope the description was understandable and clear. But, if you have any questions, then leave me your comments below! 😉
Have a nice a day! 🙂