Hi Android Devs, So how did I get here? Well, it's been a rather rough ride navigating the industry and lately I thought to myself how many beginners or junior android devs are going through my struggles on a daily. So expect a series of articles on some of the challenges that i face as a junior developer and solutions on how to solve them as simple as possible. What are you to expect in this series of articles “the simple stuff”. Now a deep dive into todays topic Room Database.
What is Room Database
Room database is database layer on top of SQLite database that takes care of mundane tasks that you used to handle with a helper class, such as
SQLiteOpenHelper. Provides easier local data storage. The Room database uses the DAO to issue queries to the SQLite database.
Before wee get into the coding, here are some of the components that you will use :
Entity: When working with Architecture Components, the entity is an annotated class that describes a database table — Basically an Entity will be our model class which will also be our table in our database.
DAO: Data access object. A mapping of SQL queries to functions. When you use a DAO, you call the methods, and the components take care of the rest.
LiveData: A data holder class that follows the observer pattern, which means that it can be observed. Always holds/caches latest version of data. Notifies its observers when the data has changed.
LiveData is lifecycle aware. UI components observe relevant data.
LiveData automatically manages stopping and resuming observation, because it's aware of the relevant lifecycle status changes.
Remember when i said the “simple stuff”, well i am keeping true to my words and for that reason i have not implemented all components i have left our repository and viewModel. we will discuss this in a future articles lets get us started.
Lets code now copy the following dependencies to your build.gradle at app level
Many are the times that I follow similar articles and end up debugging gradle issue. Worry not i got you! below is my root level build.gradle
We will create our entity class as follow not the annotations @Entity this is used for mapping our tables in the database. Other annotation expected are :
@PrimaryKey — as its name indicates, this annotation points the primary key of the entity. autoGenerate — if set to true, then SQLite will be generating a unique id for the column
@PrimaryKey(autoGenerate = true)
@ColumnInfo — allows specifying custom information about column
@ColumnInfo(name = “column_name”)
@Ignore — field will not be persisted by Room
@Embeded — nested fields can be referenced directly in the SQL queries.
Now lets create our Data access object which is an interface.
Note : In the method insertAll(vararg todo: TodoEntity) defining a parameter of a function as vararg. this will allow us to pass n number of parameters to a vararg variable of the defined datatype or even of a generic type.
Represents the database. It’s an object that holds a connection to the SQLite database, and all the operations are executed through it. It is annotated with @Database.
Now that we have created our components now it is time to link them up and see how the work. we will proceed to create our MainActivity.kt class and paste the following code:
Note: You should run the database queries of Main thread/UiThread thus we have to create a new thread. this will enable us to avoid a common error:
Cannot access database on the main thread since it may potentially lock the UI for a long periods of time.
Run the code now😁
Well that is the end of part one, my good people from above code we have work on inserting to room, in later articles we shall cover CRUD, followed but best practices and how to architect room components. Till next week keep coding,learing and making the world a better place✌️.
You can reach me on my LinkedIn :