Getting Started with Room

Overview

Android provides a set of libraries to help to design of a highly maintainable and robust code. They have provided with helper classes for better data caching and handling UI component states.

Room

The Room is a robust SQL object mapping library. It provides a layer of Abstraction over the Android’s SQLite Database. Allowing us to fluently access the power of SQLite database.

There are 3 major components in Room:

  1. Database:
    Contains the database holder and serves as the main access point for the app’s persisted relational data.
  2. Entity:
    Represents a table within the database.
  3. DAO:
    Contains the methods used for accessing the database.
Architecture Diagram

Boiler Plate Code

In this tutorial we will be making a very basic Todo Application, that will perform CRUD operations using the Room Database.

Todo App
Boilerplate Project Structure

The boilerplate code can create, update and delete a todo/memo. However, the data won’t persist as soon as the app is closed.

Our main screen is a having a recyclerview to list all of our todos. The model of the todo is saved in the model package which is as below:

[code language=”java”]

public class Todo {
private long _id;

private String todoTitle;

private String todoText;

private long timeStamp;

public long get_id() {
return _id;
}

public void set_id(long _id) {
this._id = _id;
}

public String getTodoTitle() {
return todoTitle;
}

public void setTodoTitle(String todoTitle) {
this.todoTitle = todoTitle;
}

public String getTodoText() {
return todoText;
}

public void setTodoText(String todoText) {
this.todoText = todoText;
}

public long getTimeStamp() {
return timeStamp;
}

public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
}

[/code]
We will be using this class as an Entity (Table) for our database. Through annotations, Room makes it much easier to define the properties of the table. Annotations like PrimaryKey, ColumnInfo are quite understandable by name.

A table is created using @Entity annotation and the ‘tableName’ attributes are used to provide a table name. We define primary key of the table using @PrimaryKey annotation and columns of the table using @ColumnInfo annotation and their respective names with a name attribute.

Our model class after adding annotations will look as below:

[code language=”java”]

@Entity(tableName = “todo”)
public class Todo {
@PrimaryKey
private long _id;

@ColumnInfo(name = “title”)
private String todoTitle;

@ColumnInfo(name = “text”)
private String todoText;

@ColumnInfo(name = “time”)
private long timeStamp;

public long get_id() {
return _id;
}

public void set_id(long _id) {
this._id = _id;
}

public String getTodoTitle() {
return todoTitle;
}

public void setTodoTitle(String todoTitle) {
this.todoTitle = todoTitle;
}

public String getTodoText() {
return todoText;
}

public void setTodoText(String todoText) {
this.todoText = todoText;
}

public long getTimeStamp() {
return timeStamp;
}

public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
}
[/code]
Next, We will be making our interface for performing CRUD operations.

The dao interface has to be annotated with @Dao annotation. We use @Query annotation for executing a query and @Insert, @Update and @Delete for insertion, deletion, and updating respectively.

The dao interface will finally look as below:

[code language=”java”]
@Dao
public interface AppDao {
@Query(“Select * from ” + AppConstants.DB_NAME + ” order by time desc”)
List getAllTodos();

@Query(“Select * from ” + AppConstants.DB_NAME + ” where _id = :id order by time desc”)
Todo getTodo(String id);
@Insert
void insertTodo(Todo todo);

@Update
void updateTodo(Todo todo);

@Delete
void deleteTodo(Todo todo);
}
[/code]
Attribute values can also be passed via interface method arguments and are used in queries with ‘:’ sign. As we can see in case of ‘getTodo’.

And at last, we will create an abstract Database Class that will extend RoomDatabase class.
Database class is created through @Database annotation, it is mandatory to specify entities and version of the database. In our Database class, we will add access methods of our each Dao class.

The AppDatabase class is as below:

[code language=”java”]
@Database(entities = {Todo.class}, version = 1) extends RoomDatabase {
public abstract AppDao appDao();
}
[/code]
We will also be using a Singleton class to get the reference to our AppDao interface. Which is as below:

[code language=”java”]
public class AppDBHandler {
private AppDao appdao;
private static AppDBHandler ourInstance;

public static AppDBHandler getInstance(Context context) {
if(ourInstance == null) {
ourInstance = new AppDBHandler(context);
}
return ourInstance;
}

private AppDBHandler(Context context) {
AppDatabase db = Room.databaseBuilder(context, AppDatabase.class, AppConstants.DB_NAME).build();
appdao = db.appDao();
}

public AppDao getAppDao() {
return appdao;
}
}
[/code]
At last, we are all done with setting up database boilerplate for the project. Now the only thing left to do is adding database operations in our project using our Dao interface.

The Room directly maps the database model to our Java Bean.

In our MainActivity.class, we will add following lines to retrieve the list of all todos. Just below setting adapter to our recyclerview.

[code language=”java”]
MainActivity.java


recyclerView.setAdapter(todoListAdapter);
final AppDao dao = AppDBHandler.getInstance(getApplicationContext()).getAppDao();
Thread T1 = new Thread(new Runnable() {
@Override
public void run() {
todos.addAll(dao.getAllTodos());
runOnUiThread(new Runnable() {
@Override
public void run() {
todoListAdapter.notifyItemRangeInserted(0, todos.size());
}
});
}
});
T1.start();
[/code]

Note: All the database calls in Room are synchronous and run on same thread to which they are called. If the call is a made on UI thread, then it will throw a IllegalStateException.

[code language=”java”]
MainActivity.java


case 1: {
final AppDao dao = AppDBHandler.getInstance(getApplicationContext()).getAppDao();
Thread T1 = new Thread(new Runnable() {
@Override
public void run() {
dao.deleteTodo(todos.get(position));
todos.remove(position);
runOnUiThread(new Runnable() {
@Override
public void run() {
Snackbar.make(parentLayout, “Todo Deleted”, Snackbar.LENGTH_SHORT).show();
todoListAdapter.notifyItemRemoved(position);
}
});
}
});
T1.start();
break;
}
[/code]
We are using a single Dialog for creating and editing new todos. In our logic, we are using a private Todo class reference null value of which signifies whether the action is an update or a create action. We will modify our existing logic as below:

[code language=”java”]
CreateTodoDialog.java


@Override
public void onClick(View view) {


if(this.todo == null) {
todo = new Todo();
todo.set_id(System.currentTimeMillis());
todo.setTimeStamp(System.currentTimeMillis());
todo.setTodoTitle(edtTitle.getText().toString());
todo.setTodoText(edtText.getText().toString());
final AppDao dao = AppDBHandler.getInstance(getContext()).getAppDao();
Thread T1 = new Thread(new Runnable() {
@Override
public void run() {
dao.insertTodo(todo);
dataCallbackListener.onDataReceived(todo);
}
});
T1.start();
} else {
todo.setTimeStamp(System.currentTimeMillis());
todo.setTodoTitle(edtTitle.getText().toString());
todo.setTodoText(edtText.getText().toString());
final AppDao dao = AppDBHandler.getInstance(getContext()).getAppDao();
Thread T1 = new Thread(new Runnable() {
@Override
public void run() {
dao.updateTodo(todo);
dataCallbackListener.onDataReceived(todo);
}
});
T1.start();
}
[/code]
And that’s it, the Room itself will use the provided pojo for performing insert and update operations.

Remarks

I believe that it will be very helpful in creating applications that require data caching and have well structure models for reusability. Especially, e-commerce applications in which we can model our product details like and can easily use them in carts and product info sections (Eg. quantity, favorites) where we can prevent unnecessary API calls.


Posted

in

by

Recent Post

  • Generative AI in HR Operations: Overview, Use Cases, Challenges, and Future Trends

    Overview Imagine a workplace where HR tasks aren’t bogged down by endless paperwork or repetitive chores, but instead powered by intelligent systems that think, create, and adapt—welcome to the world of GenAI. Generative AI in HR operations offers a perfect blend of efficiency, personalization, and strategic insight that transforms how organizations interact with their talent. […]

  • Generative AI in Sales: Implementation Approaches, Use Cases, Challenges, Best Practices, and Future Trends

    The world of sales is evolving at lightning speed. Today’s sales teams are not just tasked with meeting ambitious quotas but must also navigate a maze of complex buyer journeys and ever-rising customer expectations. Despite relying on advanced CRM systems and various sales tools, many teams remain bogged down by repetitive administrative tasks, a lack […]

  • Generative AI in Due Diligence: Integration Approaches, Use Cases, Challenges, and Future Outlook

    Generative AI is revolutionizing the due diligence landscape, setting unprecedented benchmarks in data analysis, risk management, and operational efficiency. By combining advanced data processing capabilities with human-like contextual understanding, this cutting-edge technology is reshaping traditional due diligence processes, making them more efficient, accurate, and insightful. This comprehensive guide explores the integration strategies, practical applications, challenges, […]

  • Exploring the Role of AI in Sustainable Development Goals (SDGs)

    Artificial Intelligence (AI) is revolutionizing how we address some of the world’s most pressing challenges. As we strive to meet the United Nations’ Sustainable Development Goals (SDGs) by 2030, AI emerges as a powerful tool to accelerate progress across various domains. AI’s potential to contribute to sustainable development is vast from eradicating poverty to combating […]

  • Future Trends in AI Chatbots: What to Expect in the Next Decade

    Artificial Intelligence (AI) chatbots have become indispensable across industries. The absolute conversational capabilities of AI chatbots are enhancing customer engagement, streamlining operations, and transforming how businesses interact with users. As technology evolves, the future of AI chatbots holds revolutionary advancements that will redefine their capabilities. So, let’s start with exploring the AI chatbot trends: Future […]

  • Linguistics and NLP: Enhancing AI Chatbots for Multilingual Support

    In today’s interconnected world, businesses and individuals often communicate across linguistic boundaries. The growing need for seamless communication has driven significant advancements in artificial intelligence (AI), particularly in natural language processing (NLP) and linguistics. AI chatbots with multilingual support, are revolutionizing global customer engagement and service delivery. This blog explores how linguistics and NLP are […]

Click to Copy