What is Architecture Components?
With the popular demand, The Android Team has written an opinionated guide to architecture android applications and developed a set of architecture components.
Architecture Components are a growing set of libraries there will be more of them coming out and they are meant for creating an Android Application in a better way.
The whole point of these libraries is to simplify things that might have been a small challenge with android mobile application development.
List of Architecture Components:
Room:
The room is a robust SQL object mapping library.
Lifecycle Components:
Lifecycle components like Live Data, Lifecycle Observers, View Model and Lifecycle owners helps you to handle your app’s life cycle.
Paging Library:
Paging library is a small library to help you to load data from server gradually.
Here, We’ll focus on Room Database.
What is Room Database?
- The room is persistence library that provides an abstraction layer over SQLite.
- So basically by using Room database, your SQLite code becomes much easier by implementing annotations. So now you don’t need to write a lot of code to save or store your data locally.
- Room database library reached 1.0 stable, so you can use this in an android application without any fear.
Why Room Database?
- Working directly with SQLite database in Android has several drawbacks like,
- You have to write a lot of boilerplate code.
- You have to implement object mapping for every single query you wrote.
- A database migration is too difficult in SQLite.
- Hard to access database.
- If you are not careful, you can easily do the long-running operation or task on the main thread.
Advantages of Room Database
- We just have to write a Little boilerplate code.
- All Entity and Query will be checked at compile time so no worries to check tables and queries that might crash the application.
- Room not only provides query error It also takes care of missing table error.
- You can fully integrate Room Database with other Architecture Components like Live Data.
Let’s get started with Room Database
Update the dependencies
Room Database libraries are available via Google’s maven repositories, So you need to add it to the list of repositories in a project level build.gradle file.
allprojects { repositories { google() jcenter() } }
In your app/build.gradle file, you need to add dependencies for room database.
implementation "android.arch.persistence.room:runtime:1.0.0" annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
Create Table with Room
@Entity(tableName = "tbl_employee") public class Employee { @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name = "name") private String name; @ColumnInfo(name = "mobile") private String mobile; }
@Entity will define a table, write table name in ‘tableName’
(Ignore if your class name is same as your table name)
@PrimaryKey will define your column as a primary key. Add ‘autoGenerate’ as a true to auto-generate value in that particular column.
@ColumnInfo is used to define that field as a table column. Write the name of your table filed in ‘name’ (Ignore if the variable name and table field name are same)
Create DAO (Database Access Object)to handle all queries
Read query
@Dao public interface EmployeeDao { @Query("SELECT * FROM tbl_employee") List getAllEmployees(); }
@Dao Contains all the methods which are used to access the database
@Query Here you can write all the Custom SQL Queries
Insert Query
@Insert void insertEmployee(Employee employee);
@Insert annotation will automatically insert data in the table
Insert Query for list of data
@Insert void insertEmployees(List employees);
@Insert If you want to add more than one employee than you can also do it by this annotation.(If you pass single record it will insert single If you pass the list of record it will insert all the records)
Update Query
@Update void updateEmployee(Employee employee);
@Update annotation used to update any of the data stored in the table
Delete Query
@Delete void deleteEmployee(Employee employee);
@Delete annotation will delete the employee data
Generate Database class to handle tables and queries
@Database(entities = {Employee.class}, version = 1) public abstract class EmployeeDB extends RoomDatabase { public abstract EmployeeDao employeeDao(); }
@Database annotation will create your database. In Entities, you have to specify all the entities(Tables) you have created.
Build database in your activity
database = Room.databaseBuilder(getApplicationContext(), EmployeeDB.class, DATABASE_NAME).allowMainThreadQueries().build();
Here you have to specify the name of Database and build the database after that you can access all Database query by object.
Retrieve all data employee
List employeeList = database.employeeDao().getAllEmployees();
By this, you can get all the employee detail as a list.
Insert Employee Data
database.employeeDao().insertEmployee(employee);
Here you have to specify employee details to insert into a table.
Update Employee Data
database.employeeDao().updateEmployee(employee);
You can update the employee details based on unique ID.
Delete Employee Data
database.employeeDao().deleteEmployee(employee);
Delete employee data from table based on unique ID.