Realm Database Android

Posted By : Chandan Wadhwa | 07-Jun-2016

realm database android

Hi Folks.

 

Today I am going to describe about realm a new database for mobile apps.

Realm is OO database for mobile applications which is the replacement of Sqlite database. Realm store data in a universal, table-based format by a C++ core which allow data access from multiple languages as well as a range of ad hoc queries.

Realm is faster for user and easy to configure for developers. And when you need power?user features, like simple migrations, built-in encryption, and more, are all provided with realm.

 

Integrating realm in android

1) Adding Dependency

 
compile 'io.realm:realm-android:0.82.1'

 

 2) Creating realm instance

To create a new Realm, you can call the static Realm.getInstance method from activity

 

Realm myRealm = Realm.getInstance(context);
 

3) Creating bean class which extends realmObject

public class Country extends RealmObject {
    private String name;
    private int population;

    public Country() { }

    public String getName() {
        return name;
    }

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

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }
}
 

4) Begin Transaction

For facilitating safe multi threading use the beginTransaction() method and to end the transaction use commitTransaction() method.

 
 5) Insert Values 
 
        myRealm.beginTransaction();
        // Create an object
        Country country1 = myRealm.createObject(Country.class);
        // Set its fields
        country1.setName("Norway");
        country1.setPopulation(5165800);
        myRealm.commitTransaction();

 

6) Select Values from table

        RealmResults<Country> results1 =
                myRealm.where(Country.class).findAll();

        for(Country c:results1) {
            Log.d("results1", c.getName());
            Log.d("results1", c.getPopulation()+"");
        }
 

 7) Display result based on query

 

    public void showSelectedRecord(View v)
    {
        RealmResults results2 =
                myRealm.where(Country.class)
                        .greaterThan("population", 7000000)
                        .findAll();
        for(Country c:results2) {
            Log.d("results1", c.getName());
            Log.d("results1", c.getPopulation()+"");
        }
    }
 

8) Updating Record

    public void updateRecord(View v)
    {
        myRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                Country country3 = realm.where(Country.class).equalTo("name", "India").findFirst();
                country3.setName("Australia");
            }
        });

    }
 

THANKS

About Author

Author Image
Chandan Wadhwa

Chandan is an Android Apps developer with good experience in building native Android applications.

Request for Proposal

Name is required

Comment is required

Sending message..