Skip to main content

How to save data to Realm Database in Kotlin

How to save data to Realm Database in Kotlin.

In this tutorial, we will learn how to save data to a Realm database in Kotlin. Realm is a mobile database that is designed to be fast, efficient, and easy to use. It provides an object-oriented API for interacting with data, making it a great choice for storing and retrieving data in your Kotlin applications.


Before we begin, make sure you have the following set up:

  • Kotlin installed on your machine
  • Android Studio or any other development environment of your choice
  • A basic understanding of Kotlin programming language

Step 1: Add Realm to Your Project

To get started, you need to add the Realm dependency to your project. Open your app-level build.gradle file and add the following dependencies:

dependencies {
implementation 'io.realm:realm-android:10.8.0'

Sync your project to download the required dependencies.

Step 2: Create a Realm Object Model

Next, we need to define a data model class that represents the object we want to store in the Realm database. This class should extend the RealmObject class provided by the Realm library.

For example, let's say we want to store a Person object with a name and age. We can define the Person class as follows:

import io.realm.RealmObject

open class Person : RealmObject() {
var name: String? = null
var age: Int? = null

Step 3: Create a Realm Instance

Before we can start saving data to the Realm database, we need to create an instance of the Realm class. You can do this by calling the Realm.getDefaultInstance() method.

val realm = Realm.getDefaultInstance()

Step 4: Save Data to the Realm Database

Once we have a Realm instance, we can use it to save data to the database. To do this, we simply create an instance of our data model class, set its properties, and then call the realm.executeTransaction() method to save the data.

Here's an example of how to save a Person object to the Realm database:

realm.executeTransaction { realm ->
val person = realm.createObject( = "John Doe"
person.age = 25

The executeTransaction() method ensures that the data is saved atomically, meaning that it will either save all changes or none at all.

Step 5: Query Data from the Realm Database

Once we have saved data to the Realm database, we can retrieve it using queries. Realm provides a powerful query API that allows us to filter and sort data based on specific criteria.

Here's an example of how to query all Person objects from the Realm database:

val persons = realm.where(
for (person in persons) {
println("Name: ${}, Age: ${person.age}")

In the above example, we use the realm.where() method to create a query that returns all Person objects. The findAll() method returns a RealmResults object that we can iterate over to access the individual objects.

Step 6: Close the Realm Instance

Once we are done working with the Realm database, it's important to close the Realm instance to free up resources. To do this, simply call the realm.close() method.


Closing the Realm instance is important to prevent memory leaks and ensure that the data is properly persisted.


In this tutorial, we learned how to save data to a Realm database in Kotlin. We covered the steps to add Realm to your project, create a Realm object model, create a Realm instance, save data to the database, query data from the database, and close the Realm instance. Realm provides a simple and efficient way to store and retrieve data in your Kotlin applications, making it a great choice for managing your app's data.