Skip to main content

How to use Realm List and Realm RealmResults in Kotlin

How to use Realm List and Realm RealmResults in Kotlin.

Here's a step-by-step tutorial on how to use Realm List and RealmResults in Kotlin:

Step 1: Add Realm to your project

First, you need to add the Realm dependency to your project. Open your project's build.gradle file and add the following lines to the dependencies block:

implementation "io.realm:realm-android-library:10.8.0"
kapt "io.realm:realm-annotations-processor:10.8.0"

Step 2: Define your Realm model class

Create a new Kotlin class that represents your data model. This class should extend the RealmObject class provided by Realm. Here's an example:

open class Person : RealmObject() {
var name: String = ""
var age: Int = 0
var hobbies: RealmList<String> = RealmList()
}

In the above example, we have a Person class with name, age, and hobbies properties. The hobbies property is of type RealmList<String>, which will be used to store a list of strings.

Step 3: Initialize the Realm instance

Before you can start using Realm, you need to initialize the Realm instance in your application's onCreate() method. Add the following code to your Application class:

class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
Realm.init(this)
}
}

Step 4: Use Realm List and RealmResults in your code

Now that you have set up Realm, you can start using Realm List and RealmResults in your code. Here are a few examples:

4.1 Creating and saving objects with Realm List:

// Get a Realm instance
val realm = Realm.getDefaultInstance()

// Create a new person object
val person = Person()
person.name = "John"
person.age = 25

// Add hobbies to the person object
person.hobbies.add("Reading")
person.hobbies.add("Gaming")

// Save the person object to the Realm database
realm.beginTransaction()
realm.copyToRealm(person)
realm.commitTransaction()

4.2 Querying objects with RealmResults:

// Get a Realm instance
val realm = Realm.getDefaultInstance()

// Query all persons from the database
val results: RealmResults<Person> = realm.where(Person::class.java).findAll()

// Iterate over the results and print the names
for (person in results) {
println(person.name)
}

4.3 Updating objects with Realm List:

// Get a Realm instance
val realm = Realm.getDefaultInstance()

// Find a person by name
val person = realm.where(Person::class.java).equalTo("name", "John").findFirst()

// Update the person's hobbies
realm.beginTransaction()
person?.hobbies?.add("Cooking")
realm.commitTransaction()

4.4 Deleting objects with RealmResults:

// Get a Realm instance
val realm = Realm.getDefaultInstance()

// Query all persons from the database
val results: RealmResults<Person> = realm.where(Person::class.java).findAll()

// Delete the first person
realm.beginTransaction()
results.deleteFirstFromRealm()
realm.commitTransaction()

That's it! You have now learned how to use Realm List and RealmResults in Kotlin. You can use these concepts to store and retrieve data efficiently in your Android applications.