Warning
This repository is no longer supported or updated. It has been migrated as part of a BoM here: kmp-realm.
kmp-realm a Kotlin Multiplaform library which wraps Kotlin Realm with pre-configuration.
1️⃣ Add the dependency in the sourceSet of all modules that require Realm:
implementation('io.github.tweener:kmp-realm:$kmp-realm_version')
2️⃣ Add the Realm plugin dependency to the root's build.gradle.kts
:
id("io.realm.kotlin").version("1.13.0").apply(false)
3️⃣ Apply the Realm plugin to all modules that declare RealmObject
s:
id("io.realm.kotlin")
1️⃣ Configure your database schema by specifying all your RealmObject
s:
val realmDatabse = RealmDatabase(
schema = setOf(
// Here, add all your classes that extend `RealmObject`
RealmUserModel::class, // This is an example. RealmUserModel extends RealmObject
...
)
)
2️⃣ Use the methods from RealmDatabase
class to interact with your Realm database:
// Get all users synchronously
realmDatabase.getAll<RealmUserModel>()
// Get all users asynchronously, using Kotlin Flows
realmDatabase.getAllAsFlow<RealmUserModel>()
// Find by property
realmDatabase.findByProperty(propertyName = "name", propertyValue = "John")
// Find by Realm UUID
realmDatabase.findByRealmUuid(uuid = "f826c548-fec7-4409-bdfb-523f29383857")
// Insert a new model or update an existing one
realmDatabase.upsert(user)
// Delete a model
realmDatabase.deleteById(id = "user1234")