Introduction to the Realm SDK for Android
Rate this tutorial
In this article, you will learn how to set up an Android application with the Realm Android SDK, write basic queries to manipulate data, and you'll receive an introduction to Realm Studio, a tool designed to view the local Realm database.
Pre-Requisites: You have created at least one app using Android Studio.
What is Realm?
Realm is an object database that is simple to embed in your mobile app. Realm is a developer-friendly alternative to mobile databases such as SQLite and CoreData.
Before we start, create an Android application. Feel free to skip the step if you already have one.
Now, let's get started on how to add the Realm SDK to your application.
Step 1: Add the gradle dependency to the project level build.gradle file:
Also, add mavenCentral as our dependency, which was previously jCenter for Realm 10.3.x and below.
Step 2: Add the Realm plugin to the app level build.gradle file:
Keep in mind that order matters. You should add the realm-android plugin after kotlin-kapt.
We have completed setting up Realm in the project. Sync Gradle so that we can move to the next step.
Step 3: Initialize and create our first database:
The Realm SDK needs to be initialized before use. This can be done anywhere (application class, activity, or fragment) but to keep it simple, we recommend doing it in the application class.
Now that we have the Realm SDK added to our project, let's explore basic CRUD (Create, Read, Update, Delete) operations. To do this, we'll create a small application, building on MVVM design principles.
The application counts the number of times the app has been opened, which has been manipulated to give an illustration of CRUD operation.
- Create app view object when opened the first time — C R U D
- Read app viewed counts—C R U D
- Update app viewed counts—C R U D
- Delete app viewed counts— C R U D
Once you have a good understanding of the basic operations, then it is fairly simple to apply this to complex data transformation as, in the end, they are nothing but collections of CRUD operations.
Before we get down to the actual task, it's nice to have background knowledge on how Realm works. Realm is built to help developers avoid common pitfalls, like heavy lifting on the main thread, and follow best practices, like reactive programming.
The default configuration of the Realm allows programmers to read data on any thread and write only on the background thread. This configuration can be overwritten with:
In this example, we keep
allowQueriesOnUiThread(true)which is the default configuration.
Let's get started and create our object class
VisitInfowhich holds the visit count:
In the above snippet, you will notice that we have extended the class with
RealmObject, which allows us to directly save the object into the Realm.
We can insert it into the Realm like this:
To read the object, we write our query as:
To update the object, we use:
And finally, to delete the object:
Realm Studio is a developer tool for desktop operating systems that allows you to manage Realm database instances.
Let's grab our database file from our emulator or real device.
Detailed steps are as follows:
Step 1: Go to Android Studio, open "Device File Explorer" from the right-side panel, and then select your emulator.
Step 2: Get the Realm file for our app. For this, open the folder named data as highlighted above, and then go to the data folder again. Next, look for the folder with your package name. Inside the files folder, look for the file named after the database you set up through the Realm SDK. In my case, it is test.db.
Step 3: To export, right-click on the file and select "Save As," and then open the file in Realm Studio.
Notice the visit count in the
VisitInfoclass (AKA table) which is equivalent to the visit count of the application. That's all, folks. Hope it helps to solve the last piece of the puzzle.