Room Library-A beginner’s guide
This is a first week blog made during GSoC’18 period under Systers Organisation.
Room is persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. It was released in Google IO’17 & has been my topic of interest ever since. Let’s learn it step wise :)
Step 1 : Update gradle.build file for dependencies
In your app/build.gradle
file, add the dependencies for Room.
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
Room’s dependencies are available via Google’s new Maven repository, add it in project level build.gradle file:
allprojects {
repositories {
google()
jcenter()
}
}
Step 2: Create entity classes
Room creates a table for each class annotated with @Entity
; the fields in the class correspond to columns in the table. Therefore, the entity classes tend to be small model classes that don’t contain any logic.For ex:
@Entity(tableName = "Accessories")
public class Accessory {
@PrimaryKey
private int accessoryId;
private String accessoryName;
private int accessoryPoints;
private int accessoryPurchased;
public Accessory(int accessoryId, String accessoryName, int accessoryPoints, int accessoryPurchased) {
this.accessoryId = accessoryId;
this.accessoryName = accessoryName;
this.accessoryPoints = accessoryPoints;
this.accessoryPurchased = accessoryPurchased;
}
}
Step 3: Create DAO classes
DAOs are responsible for defining the methods that access the database. In the initial SQLite implementation of our project, all the queries to the database were done in the LocalUserDataSource
file, where we were working with Cursor
objects. With Room, we don’t need all the Cursor
related code and can simply define our queries using annotations in the UserDao
class.
@Dao
public interface AccessoryDao {
@Insert
void insertAllAccessories(List<Accessory> hairsList);
@Query("SELECT accessoryPurchased FROM accessories WHERE accessoryId = :id")
int getPurchasedAccessories(int id);
@Query("UPDATE accessories SET accessoryPurchased = 1 WHERE accessoryId = :id")
void setPurchasedAccessories(int id);
@Query("UPDATE Accessories SET accessoryPurchased =0")
void resetAccessoryPurchase();
}
Step 4 — Create the database
We need to define an abstract class that extends RoomDatabase
. This class is annotated with @Database
, lists the entities contained in the database, and the DAOs which access them.
@Database(entities = {Accessory.class, Answer.class, Avatar.class, Clothes.class, Hair.class, Points.class, Question.class, Scenario.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract AccessoryDao accessoryDao();
}
You can also do the migrations of database.
To find out more about how to implement database migrations and how they work under the hood, check out this post:
Tada!! You just completed the whole database. You only need to use these functions inside your code by any architecture.
Thanks for reading! Be sure to click claps below to recommend this article if you liked it.