How to delete data from Realm Database in Kotlin
How to delete data from Realm Database in Kotlin.
Here is a step-by-step tutorial on how to delete data from a Realm Database in Kotlin:
Step 1: Add Realm library to your project
First, you need to add the Realm library to your project. Open your project's build.gradle
file and add the following dependency:
implementation 'io.realm:realm-android'
Step 2: Initialize Realm
Next, you need to initialize the Realm in your application's entry point. This is usually done in the onCreate
method of your Application
class. Add the following code to initialize Realm:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
Realm.init(this)
}
}
Don't forget to update your manifest file to use this custom Application
class.
Step 3: Define a Realm model
To delete data from the Realm database, you need to have a model class that represents the data you want to delete. Create a Kotlin data class that extends the RealmObject
class and define the fields you want to store. For example:
open class Person : RealmObject() {
@PrimaryKey
var id: String = ""
var name: String = ""
var age: Int = 0
}
Step 4: Delete data from Realm
To delete data from Realm, you need to open a Realm instance, start a write transaction, and then delete the desired objects. Here's an example of how to delete a single object:
// Open a Realm instance
val realm = Realm.getDefaultInstance()
// Start a write transaction
realm.beginTransaction()
// Find the object you want to delete
val personToDelete = realm.where(Person::class.java).equalTo("id", "123").findFirst()
// Delete the object
personToDelete?.deleteFromRealm()
// Commit the transaction
realm.commitTransaction()
// Close the Realm instance
realm.close()
In the above example, we open a Realm instance using Realm.getDefaultInstance()
, start a write transaction using realm.beginTransaction()
, find the object we want to delete using a query, delete the object using deleteFromRealm()
, and then commit the transaction using realm.commitTransaction()
. Finally, we close the Realm instance using realm.close()
.
Step 5: Delete multiple objects
If you want to delete multiple objects, you can use the realm.where()
method with a query to find the objects you want to delete, and then delete them one by one or all at once. Here's an example of how to delete multiple objects:
// Open a Realm instance
val realm = Realm.getDefaultInstance()
// Start a write transaction
realm.beginTransaction()
// Find the objects you want to delete
val personsToDelete = realm.where(Person::class.java).equalTo("age", 25).findAll()
// Delete the objects one by one
for (person in personsToDelete) {
person.deleteFromRealm()
}
// Alternatively, delete all objects at once
personsToDelete.deleteAllFromRealm()
// Commit the transaction
realm.commitTransaction()
// Close the Realm instance
realm.close()
In the above example, we find the objects to delete using a query (equalTo("age", 25)
), and then delete them one by one using a loop or delete all at once using deleteAllFromRealm()
.
That's it! You have successfully learned how to delete data from a Realm Database in Kotlin.