1 / 1
Sep 2024

Hi,

I’m currently using Kotlin Multiplatform and Device Sync to access my Realm collections. However, my queries don’t seem to reach the server when I query an object using the ObjectId :

java.util.NoSuchElementException: List is empty. at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:221) at data.realm.RealmDB.getRecommendation(RealmDB.kt:102)

I recently upgraded my project to Kotlin 2.0, so I use realm version to 2.0.0 as well.

Here is what my data class looks like :

@Serializable class Recommendation: RealmObject { @PrimaryKey @PersistedName("_id") var id: ObjectId = ObjectId() var name: String = "" var products: RealmList<String> = realmListOf() companion object }

My Realm object configuration :

init { runBlocking { user = app.login(Credentials.anonymous()) } configureRealm() } private fun configureRealm() { val config = SyncConfiguration.Builder( user = user, schema = schemaClasses ).schemaVersion(0) .initialSubscriptions() { add(it.query<Recommendation>()) add(it.query<data.models.User>("_id == $0", ObjectId(user.id))) } .build() realm = Realm.open(config) }

The performed query to my collection. I hard coded the ID for testing :

override fun getRecommendation(id: ObjectId): Recommendation { println("Performing query for recommendation with id: $id") return realm.query<Recommendation>("_id == $0", ObjectId("66db33beb29eaf61e4e4988b")).find().first() }

and finally my JSON table :

[{ "_id": { "$oid": "66db33beb29eaf61e4e4988b" }, "name": "picksOfTheMonth", "products": [ ...] }]

Just in case, my gradle configuration :

library-base = { module = "io.realm.kotlin:library-base", version= "2.0.0" } library-sync = { module = "io.realm.kotlin:library-sync", version= "2.0.0" } kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "1.8.0" }
commonMain.dependencies { // Realm implementation(libs.library.base) implementation(libs.library.sync) implementation(libs.kotlinx.coroutines.core) }

It seems to me like there might be something missing with the way I configured the Realm. When I used the version 1.16.0 I was able to make it sync without any problem.

I really appreciate your help in advance !