This project is to understand Room and its implementation using MVVM app architecture.

Room is an object-mapping library that provides local data persistence with minimal boilerplate code. At compile time, it validates each query against your data schema, so broken SQL queries result in compile-time errors instead of runtime failures. Room abstracts away some of the underlying implementation details of working with raw SQL tables and queries. It also allows you to observe changes to the database’s data, including collections and join queries, exposing such changes using LiveData objects. It even explicitly defines execution constraints that address common threading issues, such as accessing storage on the main thread.

Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

To-do-list App overview

MVVM Architecture:

The below diagram shows how Room forms an abstract layer over the database. This helps ViewModel to be unaware of the activity lifecycle and helps in testing each functionality separately.

Image for post

MVVM architecture and Room

There are basically 6 steps to implement Room persistence library in Android

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

//liveData and Room
implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "androidx.lifecycle:lifecycle-common-java8:2.2.0"

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

The entity class contains all the columns in the database and should be annotated with @Entity. if no specific table name is given, the class name will be the table name, else we can specify the table name(tableName = “task”). If the class has two constructors we can ignore a constructor with the annotation @ignore. Column names can also be different from the variable name. This is done with the annotation @ColumnInfo(name=” name ”). Every database table requires a primary key, which is usually the row id. If we want the primary key to auto-increment we can use the annotation @PrimaryKey(autoGenerate = true), else just say @PrimaryKey and increment it ourselves. It is best practice to allow the library to do it to avoid errors.

import java.util.Date;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

@Entity(tableName = "task")
public class TaskEntry {

    @PrimaryKey(autoGenerate = true)
    private int id;
    private String description;
    private int priority;
    @ColumnInfo(name = "updated_at")
    private Date updatedAt;

    @Ignore
    public TaskEntry(String description, int priority, Date updatedAt) {
        this.description = description;
        this.priority = priority;
        this.updatedAt = updatedAt;
    }

    public TaskEntry(int id, String description, int priority, Date updatedAt) {
        this.id = id;
        this.description = description;
        this.priority = priority;
        this.updatedAt = updatedAt;
    }

  //Setter and Getters
}

#android-app-development #room #mobile-app-architecture #database #mvvm

To-do-List App Using Room and MVVM architecture
17.30 GEEK