Realm for Android

Posted By : Sobraj Yumkhaibam | 25-Mar-2016

 

As the trend for new and latest trends in software development rises to an exponential level, it's always good to get hold of something better than the normal existing trend. A piece of the latest trends is the Realm for Android. I haven't used it extensively but in a few apps i have tried it and i find it much better and way more cool than the SQLite database.

The tutorial with Realm v0.84.1

Add the following  compile dependency in the app module’s build.gradle file.

A Realm seems indifferent to a SQLite database. It has an associated file, that will persist on Android’s file system once it's created.

To create a new Realm, the static Realm.getInstance method is called from inside any Activity.

Note that a Realm file called default.realm is created when calling Realm.getInstance,without a RealmConfiguration is passed to it.

To add another Realm to the app, use aRealmConfiguration.Builder object and give the Realm file a unique name.

A JavaBean can be stored in a Realm when it extends the RealmObject class. Instances of the following class is easily stored in a Realm:

To use a member variable of a RealmObject as a primary key, use the @PrimaryKey annotation. For example, adding a primary key called code to the Country class:

Writing data to Realm is slightly more complex. Realm is ACID compliant and so as to ensure atomicity and consistency, Realm forces you to execute all write operations inside a transaction. The beginTransaction method is used to start a new transaction. To end the transaction, use the commitTransaction method.Here’s an example to create and save an instance of the Country class:

Notice that country1 was not created using the constructor of theCountry class. To manage an instance of a RealmObject, the createObject method must be used to create the instance.

If the constructor must be used, use the copyToRealm method of the associated Realm object before the transaction is committed. For example:

To create a query, the where method of the relevant Realm object is used and the class of the objects we are interested in, is passed. After creating the query, we can fetch all results using the findAll method, which returns a RealmResults object. In the following example, we fetch and print all objects of type Country:

To filter the results, Realm offers several methods, such as beginsWithendsWith,lesserThan and greaterThan. The following code tells how to use the greaterThan method to fetch only those Countryobjects whose population is greater than 500 million:

If the results of the query are to be sorted, use the findAllSortedmethod. It takes a String specifying the name of the field to sort by and a boolean specifying the sort order as its arguments.

 In this small article, i've provided a glimpse of how Realm can be implemented in Android. To know more about it, refer its Java documentation.

About Author

Author Image
Sobraj Yumkhaibam

Sobraj has been developing Android applications for a while now and is expert in mobile application development . He excels in developing mobile applications with location based intelligence. He loves Modelling as a hobby.

Request for Proposal

Name is required

Comment is required

Sending message..