blog_realm_database

Realm Database Tutorial For Android


Realm is a mobile first database that is built from the ground-up to run directly inside phones, tablets, and wearable. Realm database does not run on top of SQLite, it is not an ORM, it is a Realm. Quoting the official Realm documentation, “Realms are our equivalent of a database: they contain different kinds of objects, and map to one file on disk”. So, Realms are databases that do not require a separate mapping from Java objects to the persisted version on the disk.

 

Prerequisites

  • Android Studio version 1.5.1 or higher
  • JDK version 7.0 or higher
  • A recent version of the Android SDK
  • Android API Level 9 or higher (Android 2.3 and above)

 

Installation

Realm is installed as a Gradle plugin.

Step 1: Add the class path dependency to the project level build.gradle file.

Step 2: Apply the realm-android plugin to the top of the application level build.gradle file.

Once these two changes are made, simply refresh your gradle dependencies.

 

When the app is launched we have to initialize realm. It should be done only once.

You configure Realm with a RealmConfiguration object and a good place to perform this initialization in a custom application class.

 

Create RealmObject

Model classes are used for storing data in realm. A realm model class can be created by extending RealmObject class.

 

While reading data from a Realm is very simple, as you will see in the next step, writing data to it is slightly more complex. Realm is ACID compliant and to ensure atomicity and consistency, Realm forces you to execute all write operations inside a transaction.

To start a new transaction, use the beginTransaction method. Similarly, to end the transaction, use the commitTransaction method.

Here’s how you would create and save an instance of the AndroidOSModel class:

You might have noticed that osModel was not created using the constructor of the AndroidOSModel class. For a Realm to manage an instance of a RealmObject, the instance must be created using the createObject method.

If you must use the constructor, don’t forget to use the copyToRealm method of the relevant Realm object before you commit the transaction.

We can also create an instance of the AndroidOSModel class from Json data:

 

Realm offers a very intuitive and fluent API for creating queries. To create a query, use the where method of the relevant Realm object and pass the class of the objects you are interested in. After creating the query, you 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 AndroidOSModel :

Realm offers several aptly named methods, such as beginsWith, endsWith, lesserThan and greaterThan, you can use to filter the results. The following code shows you how you can use the greaterThan method to fetch only those AndroidOSModel objects whose api level is greater than 16:

If you want the results of the query to be sorted, you can use the findAllSorted method. As its arguments, it takes a String specifying the name of the field to sort by and specifying the sort order.

 

Update Values:

For updating a record first fetch it and then change the value of object using setter methods. Suppose you want to change the name of AndroidOSModel with api level 22 then it can be done in following way.

 

Delete Record:

Realm provides several methods for deleting a record.

 

Source:

MyApplication.java

activity_main.xml

MainActivity.java

list_item.xml

AndroidOsListAdapter.java

AndroidOSModel.java

 

ScreenShots:

realm_database_samplerealm_database_queryrealm_database_order

SourceCode:

Download SourceCode

 

Conclusion:

I hope you will like the article and it will definitely help you to make your apps more productive. In case of any confusion or query, feel free to ask. And if you found this tutorial to be useful, Share with others.


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.