07 August 2016

Android Realm Db Tutorial

These Days I have worked with android realm db project. So After done the project I thought It is better to write post about Realm db with android.

What Is Realm ??


Realm Db is No SQL framework which support android, ios to persistence data in mobile devices. When I start develop application with android there were few frameworks which support persistence. I worked with sqlite but the problem is It is hard to understand and write data and retrieve data.  But Realm db is the best android persistence framework I have ever seen. The main advantage is there is no sql syntax or queries . It is easy to understand  and implement.


This Diagram shows the simplicity of Realm db.



Configuration for android realm project

Add the following dependency to main project.


1
classpath "io.realm:realm-gradle-plugin:1.1.0"

Add the following dependency to sub project build.gradle file.
   
   Add the following line top of the build gradle file.

1
apply plugin: 'realm-android'

How to use Realm

Realm can be used any insert,update,delete operations in database. But unlikely sqlite realm gives easy way to handle data base operations . In here I will not show all the things in realm because realm documentation is better than my blog. So first we need realm object because realm object is a main concept in realm.

Let's say I need to add sales merchants to sales merchant table . First I create merchant object like this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

public class SalesMerchant extends RealmObject {

    @PrimaryKey    private String id;

    private String name;

    private String address;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
Let's insert first data to realm db. First I will add the db 
configuration class to my project.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import android.content.Context;

import io.realm.Realm;
import io.realm.RealmConfiguration;


public class DBConfiguration {

    private static String REALM_NAME = "sajith-blog.realm";

    public Realm getRealm(Context context) {
        Realm myOtherRealm = Realm.getInstance(
                new RealmConfiguration.Builder(context)
                        .name(REALM_NAME)
                        .build()
        );
        return myOtherRealm;
    }

}
Then Final stage write factory class to add data to database.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import domain.db.SalesMerchant;
import io.realm.Realm;


public class MerchantFactory {
    private Realm realm;

    public MerchantFactory(Realm realm) {
        this.realm = realm;
    }

    public void insertMerchant(final SalesMerchant salesMerchant) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override            public void execute(Realm bgRealm) {
                SalesMerchant addProduct = bgRealm.createObject(SalesMerchant.class);
                addProduct.setName(salesMerchant.getName());
                addProduct.setId(salesMerchant.getId());
                addProduct.setAddress(salesMerchant.getAddress());
            }
        });
    }

}
And that is it now you can see your data stored in database.
It is easy to handle transactions in realm db.
This tutorial is simple introduction of realm db. I just want to show how easy to use realm  and next tutorial i hope to go deep how to handle relationships in realm objects , Handle in different threads ,how to automatically populate db change in UI thread and other advanced techniques .