Categories
Studio

Room Persistence Library: Implementation

After fiddling with Room library for two days, Finally, I learned to implement it the right way. Google’s Codelabs is not a friendly way to learn new things rather you can read those tutorials after leaning a new thing.

I got to learn to implement this library with the help of these sources:

  1. Youtube (This guys tutorials are such simple)
  2. Mindorks on Medium (To know Room in detail)
  3. Finally, Google Codelabs.

Implementation

Write the below lines in your project level gradle.

allprojects {
    repositories {
        google()
        maven { url "https://maven.google.com" }
    }
}

Install the Room library in app-level gradle. (At the time of writing this post, 1.1.1 was the latest version)

//Room
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"

Create Entity

This is just another class of variables which defines the data for a list. Like you want to have an ArrayList with Custom data, that data class will form your entity. In this example, Alarma.java is our Custom Data Class for ArrayList.

The below code is the code before converting it to an Entity for Room.

public class Alarms
{
private String alarmTimeString;

private Boolean alarmOnOff;
private int hour;
private int minute;

public Alarms(String alarmTimeString, Boolean alarmOnOff, int hour, int minute) {
this.alarmTimeString = alarmTimeString;
this.alarmOnOff = alarmOnOff;
this.hour = hour;
this.minute = minute;
}

public String getAlarmTimeString() {
return alarmTimeString;
}


public Boolean getAlarmOnOff() {
return alarmOnOff;
}

public int getHour() {
return hour;
}

public int getMinute() {
return minute;
}
}

To convert the above class into an Entity, annotate the whole class with @Entity. Here we will also write the table name.

Define an id (int) as PrimaryKey or choose any variable as a primary key. Here I create another variable int id and annotate it with @PrimaryKey.

(Optional): You can optionally define SQLite column names or leave them as they are. In the example, I leave them as they are. Here is the code after conversion.

@Entity (tableName = "alarm_table")
public class Alarms
{
@PrimaryKey (autoGenerate = true)
int id;

private String alarmTimeString;
private Boolean alarmOnOff;
private int hour;
private int minute;

public Alarms(String alarmTimeString, Boolean alarmOnOff, int hour, int minute) {
this.alarmTimeString = alarmTimeString;
this.alarmOnOff = alarmOnOff;
this.hour = hour;
this.minute = minute;
}

public String getAlarmTimeString() {
return alarmTimeString;
}

public Boolean getAlarmOnOff() {
return alarmOnOff;
}

public int getHour() {
return hour;
}

public int getMinute() {
return minute;
}
public int getId() {
return id;
}

}
Android Room Persistense Library - Implement, Save, Delete, Update, Query

Dao

Dao stands for Data Access Objects. This is a java interface in which we define our SQLite queries. The whole purpose of Room Persistence library is to have a safe wrapper which can check the SQLite queries. In our example, let’s call it MyDao.

  1. Create a new interface and annotate it with Dao.
  2. Define the queries you need to perform for your app.
  3. For our Alarm Example, we will require these queries: Insert, Query, Delete, and Update. Here’s the code
@Dao
public interface MyDao
{
@Query("SELECT * FROM alarm_table ORDER BY alarmTimeString ASC")
List<Alarms> getAlarms();

@Query("DELETE FROM alarm_table WHERE id = :id")
void deleteAlarm(int id);

@Insert
void insert(Alarms);

@Update()
void updateAlarm(Alarms alarms);
}

AppDatabase

Now create an app database which is responsible for defining the tables and its versions as well as building the database. Here’s our code:

@Database(entities = {Alarms.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase
{
public abstract MyDao myDao();

private static volatile AppDatabase INSTANCE;

public static AppDatabase getDatabase(Context context)
{
if (INSTANCE == null)
{
synchronized (AppDatabase.class)
{
if(INSTANCE == null)
{
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "alarmdb")
.fallbackToDestructiveMigration()
.build();
}
}
}

return INSTANCE;
}
}

Here we used “fallbackToDestructiveMigration()” because in case you need to change the version numbers, this will come in handy for migrations. Basically, It’ll delete all the previous saved database from previous version. (Can be proven dangerous, use it with precaution and in development only)

That’s it for the implementation part. Read the next part of this post to know how to insert alarms into the database, how to delete an alarm from the database, how to init db, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *