I’m having an issue with `import io.realm.query` i...
# realm
j
I’m having an issue with
import io.realm.query
it is not resolved. I do have access to
realm.write { … }
but not
realm.query<MyObject>()
. And I do have
id("io.realm.kotlin") version "0.8.2"
and
Copy code
val commonMain by getting {
    dependencies {
        implementation("io.realm.kotlin:library-base:0.8.2")
    }
}
in my
build.gradle.kts
. Am I missing something there?
n
Hi @jean it's actually
Copy code
import io.realm.objects

realm.objects<MyObject>().query("your query")
You can see updated samples using
0.8.2
here (query examples: https://github.com/realm/realm-kotlin-samples/blob/main/Bookshelf/shared/src/commonMain/kotlin/io/realm/sample/bookshelf/database/RealmDatabase.kt)
c
The updated query API is only just merged and will be part of the upcoming
0.9.0
-release. If you already want to try it out you can use the
0.9.0-SNAPSHOT
following the "Using Snapshots" from the readme in the root of the repository.
j
I get the following error when I try to use the snapshot version
Could not resolve io.realm.kotlin:library-base:0.9.0-SNAPSHOT
. I guess this section of the documentation needs to be updated since it foes not work with
0.8.2
? https://github.com/realm/realm-kotlin#query
c
Did you remember to add the snapshot repository not only for the build scripts dependencies but also for the project dependencies?
j
I found that out yes. But now I’m running in this error :
Copy code
java.lang.ExceptionInInitializerError
	at io.realm.RealmConfiguration$Builder.build(RealmConfiguration.kt:61)
	at io.realm.RealmConfiguration$Companion.with(RealmConfiguration.kt:85)
	at no.myleadership.common.database.DatabaseTest$should create and init database$1.invokeSuspend(DatabaseTest.kt:22)
	at no.myleadership.common.database.DatabaseTest$should create and init database$1.invoke(DatabaseTest.kt)
	at no.myleadership.common.database.DatabaseTest$should create and init database$1.invoke(DatabaseTest.kt)
	at no.myleadership.common.TestUtilsAndroidKt$runBlockingTest$1.invokeSuspend(TestUtilsAndroid.kt:13)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.NullPointerException: RELEASE must not be null
	at io.realm.internal.platform.SystemUtilsAndroidKt.<clinit>(SystemUtilsAndroid.kt:8)
	... 11 more
for this simple test
Copy code
class DatabaseTest {

    class Frog : RealmObject {
        var name: String = ""
        var age: Int = 0
        var species: String? = null
        var owner: String? = null
    }

    @Test
    fun `should create and init database`() = runBlockingTest {
        val config = RealmConfiguration.with(schema = setOf(Frog::class))
        val realm = Realm.open(config)
    }
}
c
Are you running it as unit tests or Android instrumentation tests? Things that depend on the android library needs to run on a device/emulator. If you want to run pure JVM unit tests, you can setup a KMM project with a
jvm()
target.
j
pure unit test in the
commonTest
of a kmm project targeting android and ios. I’ll add the tests in
jvmTest
and see how it goes 🙂
171 Views