AndroidPub
Published in

AndroidPub

5 steps to implement Room persistence library in Android

Room is a persistence library, part of the Android Architecture Components. The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

Even though I am late to the party, I recently spent some time working with the Room database in Android and I wanted to highlight some basics to the implementation.

So let’s begin!

Step 1: Add following library and annotation processor to your app gradle file

implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'

Note: The reason why annotation processor is needed is because all operations like Insert, Delete, Update etc are annotated.

Step 2: Component 1 in room — Create an entity class:

This is nothing but a model class annotated with @Entity where all the variable will becomes column name for the table and name of the model class becomes name of the table. The name of the class is the table name and the variables are the columns of the table.

@Entity
public class Note implements Serializable {

@PrimaryKey(autoGenerate = true)
private int id;

private String title;
private String description;


@ColumnInfo(name = "created_at")
@TypeConverters({TimestampConverter.class})
private Date createdAt;

@ColumnInfo(name = "modified_at")
@TypeConverters({TimestampConverter.class})
private Date modifiedAt;

private boolean encrypt;
private String password;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Date getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Date getModifiedAt() {
return modifiedAt;
}

public void setModifiedAt(Date modifiedAt) {
this.modifiedAt = modifiedAt;
}

public boolean isEncrypt() {
return encrypt;
}

public void setEncrypt(boolean encrypt) {
this.encrypt = encrypt;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

Step 3: Component 2 in room — Create a DAO class:

This is an interface which acts is an intermediary between the user and the database. All the operation to be performed on a table has to be defined here. We define the list of operation that we would like to perform on table.

@Dao
public interface DaoAccess {

@Insert
Long insertTask(Note note);


@Query("SELECT * FROM Note ORDER BY created_at desc")
LiveData<List<Note>> fetchAllTasks();


@Query("SELECT * FROM Note WHERE id =:taskId")
LiveData<Note> getTask(int taskId);


@Update
void updateTask(Note note);


@Delete
void deleteTask(Note note);
}

Step 4: Component 3 in room — Create Database class:

his is an abstract class where you define all the entities that means all the tables that you want to create for that database. We define the list of operation that we would like to perform on table.

@Database(entities = {Note.class}, version = 1, exportSchema = false)
public abstract class NoteDatabase extends RoomDatabase {

public abstract DaoAccess daoAccess();
}

Step 5: Create the Repository class:

A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. We access the database class and the DAO class from the repository and perform list of operations such as insert, update, delete, get.

public class NoteRepository {

private String DB_NAME = "db_task";

private NoteDatabase noteDatabase;
public NoteRepository(Context context) {
noteDatabase = Room.databaseBuilder(context, NoteDatabase.class, DB_NAME).build();
}

public void insertTask(String title,
String description) {

insertTask(title, description, false, null);
}

public void insertTask(String title,
String description,
boolean encrypt,
String password) {

Note note = new Note();
note.setTitle(title);
note.setDescription(description);
note.setCreatedAt(AppUtils.getCurrentDateTime());
note.setModifiedAt(AppUtils.getCurrentDateTime());
note.setEncrypt(encrypt);


if(encrypt) {
note.setPassword(AppUtils.generateHash(password));
} else note.setPassword(null);

insertTask(note);
}

public void insertTask(final Note note) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
noteDatabase.daoAccess().insertTask(note);
return null;
}
}.execute();
}

public void updateTask(final Note note) {
note.setModifiedAt(AppUtils.getCurrentDateTime());

new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
noteDatabase.daoAccess().updateTask(note);
return null;
}
}.execute();
}

public void deleteTask(final int id) {
final LiveData<Note> task = getTask(id);
if(task != null) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
noteDatabase.daoAccess().deleteTask(task.getValue());
return null;
}
}.execute();
}
}

public void deleteTask(final Note note) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
noteDatabase.daoAccess().deleteTask(note);
return null;
}
}.execute();
}

public LiveData<Note> getTask(int id) {
return noteDatabase.daoAccess().getTask(id);
}

public LiveData<List<Note>> getTasks() {
return noteDatabase.daoAccess().fetchAllTasks();
}
}

Note: DO NOT PERFORM OPERATION ON MAIN THREAD AS APP WILL CRASH

And that’s it! You are now setup to use Room in your app.

Now let’s look at some basic examples on how to use it.

Sample Implementation of basic CRUD operations using ROOM

1. Insert:

NoteRepository noteRepository = new NoteRepository(getApplicationContext());
String title = "This is the title of the third task";
String description = "This is the description of the third task";
noteRepository.insertTask(title, description);

2. Update:

NoteRepository noteRepository = new NoteRepository(getApplicationContext());
Note note = noteRepository.getTask(2);
note.setEncrypt(true);
note.setPassword(AppUtils.generateHash("Password@1"));
note.setTitle("This is an example of modify");
note.setDescription("This is an example to modify the second task");

noteRepository.updateTask(note);

3. Delete:

NoteRepository noteRepository = new NoteRepository(getApplicationContext());
noteRepository.deleteTask(3);

4. Get all notes:

NoteRepository noteRepository = new NoteRepository(getApplicationContext());

noteRepository.getTasks().observe(appContext, new Observer<List<Note>>() {
@Override
public void onChanged(@Nullable List<Note> notes) {
for(Note note : notes) {
System.out.println("-----------------------");
System.out.println(note.getId());
System.out.println(note.getTitle());
System.out.println(note.getDescription());
System.out.println(note.getCreatedAt());
System.out.println(note.getModifiedAt());
System.out.println(note.getPassword());
System.out.println(note.isEncrypt());
}
}
});

5. Get single note by id:

NoteRepository noteRepository = new NoteRepository(getApplicationContext());
Note note = noteRepository.getTask(2);

Bonus: How to add date/time values to Room Db?

Room does not provide any support for storing date/time, instead it provides the extensible @TypeConverter annotation, which converts a custom class to and from a known type that Room can persist. For example, in order to store Date, we can write the following TypeConverter to store the equivalent Text in the database.

public class TimestampConverter {

private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@TypeConverter
public static Date fromTimestamp(String value) {
if (value != null) {
try {
TimeZone timeZone = TimeZone.getTimeZone("IST");
df.setTimeZone(timeZone);
return df.parse(value);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
} else {
return null;
}
}


@TypeConverter
public static String dateToTimestamp(Date value) {
TimeZone timeZone = TimeZone.getTimeZone("IST");
df.setTimeZone(timeZone);
return value == null ? null : df.format(value);
}
}

And that’s it! You can check out the sample app in Github.

Well thats it for this article. If you like this and think I have done a good job here then please don’t forget to clap. Your appreciation is valuable and keeps me motivated.

Happy Coding, Cheers!!

--

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Recommended from Medium

The Weekly Scrum – What It Is, How It Works, and Why It’s Fantastic

The Weekly Scrum — What It Is, How It Works, and Why It’s Fantastic

An Overview of MongoDB & Mongoose

AMA: SAKURA COMMUNITY X ANPANSWAP

Dependency Inversion Principle Applied To Controllers With ASP.NET MVC and StructureMap

Affordable Ways to Turn Your Website into the App

ML in Production with SageMaker — Part 2

Kubernetes Readiness Probes — Examples & Common Pitfalls

VLSI Training Institutes in Bangalore — Cranes Varsity

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anitaa Murthy

Anitaa Murthy

Android Developer @ Automattic https://github.com/anitaa1990

More from Medium

Automate your Android build code

Modern data storage on Android: Meet Jetpack DataStore — Part 2/2

Custom Notification with Work manager for Android

Android — Intercept POST form data inside of WebView