[JAVA] Database implementation using Realm

Here is a summary of how to implement the basic Realm. This time, I will explain using the code of the application I am currently creating.

⒈ Advance preparation

First of all, add the classpath to your build.gradle file.

uildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath "io.realm:realm-gradle-plugin:5.1.0"
    }
}

This is the part added by "classpath" io.realm: realm-gradle-plugin: 5.1.0 "" in the above code.

⒉ Object definition

Next, create a subclass of RealmObject and define the object.

public class Recipe extends RealmObject {

    @PrimaryKey
    int id;
    @Required
    private String name;
    @Required
    private String cookingTime;

Using a primary key makes objects a little slower to create and update, but faster to find. You cannot specify multiple fields as the primary key.

After defining, prepare getters and setters.

public int getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCookingTime(String cookingTime) {
        this.cookingTime = cookingTime;
    }

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


}

⒊ Model definition

Next, define the model.

Create new and update edits as follows. Models with a primary key can use copyToRealmOrUpdate () to create or update objects.

public class RecipeModel {

    private Realm mRealm = Realm.getDefaultInstance();

    /**
     *New recipe creation or edit update
     *
     * @param recipeId Recipe id
     * @param recipeName Recipe name
     * @param cookingTime Cooking time
     */
    public void copyOrUpdate(@Nullable Integer recipeId, String recipeName, String cookingTime) {
        if (recipeId == null) {
            recipeId = makeRecipeId();
        }
        Recipe recipe = new Recipe();
        recipe.setId(recipeId);
        recipe.setName(recipeName);
        recipe.setCookingTime(cookingTime);

        mRealm.beginTransaction();

        mRealm.copyToRealmOrUpdate(recipe);

        mRealm.commitTransaction();
    }

If id is null, create a new one.

Delete it as follows.

    /**
     *Delete recipe
     *
     * @param recipeId Recipe id
     */
    public void deleteRecipe(int recipeId) {
        mRealm.beginTransaction();

        //From the list of recipes, retrieve the first recipe with an id that matches recipeId
        Recipe recipe = mRealm.where(Recipe.class).equalTo("id", recipeId).findFirst();
        if (recipe != null) {
            recipe.deleteFromRealm();
        }

        mRealm.commitTransaction();
    }

Summary

① Add classpath to build.gradle file ② Object definition ③ Model definition

Realm makes it easy to implement a database in these three steps. Please try by all means try.

Recommended Posts

Database implementation using Realm
Implementation of validation using regular expressions
Sample code for Singleton implementation using enum
Implementation of user authentication function using devise (2)
Implementation of user authentication function using devise (1)
Implementation of tabs using TabLayout and ViewPager
Batch implementation in RubyOnRails environment using Digdag
Try using JobScheduler's REST-API --Java RestClient implementation--