realm-kotlin icon indicating copy to clipboard operation
realm-kotlin copied to clipboard

Query with multiple values

Open destman opened this issue 3 years ago • 3 comments

I am trying to build a query that will return objects with given set of values of _id property. In Java i use in opertaion and it works good. In Kotlin i try to use this query:

realm.query<ModelBookmark>("_id in $0", ids)

But it crashes with message that type not supported. Right now i generate query that looks like "_id == "1" or _id == "2" ..." and it works... Version of realm-kotlin is 1.1.0-SNAPSHOT (in 1.0.1 ByteArray still not works).

destman avatar Jul 14 '22 11:07 destman

Hi @destman, could you supply the details of the crash and the type information of _id and ids

rorbech avatar Jul 18 '22 07:07 rorbech

_id is a String (@PrimaryKey var _id: String?) ids is array of strings (Array<String>)

Crashlog: kotlin.NotImplementedError: An operation is not implemented: Unsupported type for capiRealmValueArrayat io.realm.kotlin.internal.interop.RealmInteropKt.capiRealmValue-28b4FhY(RealmInterop.kt:1518) at io.realm.kotlin.internal.interop.RealmInteropKt.access$capiRealmValue-28b4FhY(RealmInterop.kt:1) at io.realm.kotlin.internal.interop.MemScope.managedRealmValue-28b4FhY(RealmInterop.kt:1434) at io.realm.kotlin.internal.interop.RealmInterop$realm_query_parse$1.invoke(RealmInterop.kt:1051) at io.realm.kotlin.internal.interop.RealmInterop$realm_query_parse$1.invoke(RealmInterop.kt:1049) at io.realm.kotlin.internal.interop.RealmInteropKt.memScope(RealmInterop.kt:1451) at io.realm.kotlin.internal.interop.RealmInteropKt.access$memScope(RealmInterop.kt:1) at io.realm.kotlin.internal.interop.RealmInterop.realm_query_parse-6CC_B8E(RealmInterop.kt:1049) at io.realm.kotlin.internal.query.ObjectQuery$parseQuery$1.invoke(ObjectQuery.kt:175) at io.realm.kotlin.internal.query.ObjectQuery$parseQuery$1.invoke(ObjectQuery.kt:174) at io.realm.kotlin.internal.query.ObjectQuery.tryCatchCoreException(ObjectQuery.kt:184) at io.realm.kotlin.internal.query.ObjectQuery.parseQuery(ObjectQuery.kt:174) at io.realm.kotlin.internal.query.ObjectQuery.<init>(ObjectQuery.kt:61) at io.realm.kotlin.internal.query.ObjectQuery.<init>(Unknown Source:0) at io.realm.kotlin.internal.InternalTypedRealm$DefaultImpls.query(InternalTypedRealm.kt:35) at io.realm.kotlin.internal.RealmImpl.query(RealmImpl.kt:158) at com.bodunov.galileo.bottomDetails.BookmarksBottomDetails$updateContent$1.invokeSuspend(BookmarksBottomDetails.kt:128) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

destman avatar Jul 18 '22 07:07 destman

Thanks.

We are currently not able to pass list as query arguments, but support for this is on its way in https://github.com/realm/realm-core/pull/5663, so should be possible in a foreseeable future.

rorbech avatar Jul 19 '22 07:07 rorbech

Hi! The PR was merged 3 months ago, any plans on fixing this? Just got a runtime crash after implementing the query exactly as this was said in the docs

kotlin.NotImplementedError: An operation is not implemented: Unsupported type for capiRealmValue `ArrayList`

For the time being, what's the workaround?

Nek-12 avatar Oct 19 '22 15:10 Nek-12

Note, this is partly supported today using this workaround:

        val ids = listOf(1, 2)
        val queryValues = ids.joinToString(separator = ",", prefix = "{", postfix = "}")
        val result = realm.query(Person::class, "id IN $queryValues").find()
        assertEquals(2, result.size)

I.e. you can manually create the required syntax of { value1, value2, .... } and inject it into the query string.

But this issue covers using the more concise syntax of:

        val ids = listOf(1, 2)
        val result = realm.query(Person::class, "id IN $0", ids).find()

cmelchior avatar Apr 24 '23 06:04 cmelchior

@cmelchior unfortunately it doesn't work if you have ' in the ids 😕

JorisBodin avatar Apr 24 '23 09:04 JorisBodin

Yes, escaped strings are unfortunately not working great in that case. You can do something like this:

        val queryValues = listOf("id'1", "id'2").joinToString(separator = ", ", prefix = "{", postfix = "}") {
            "\"$it\""
        }

But that only works if the strings don't contain ".

cmelchior avatar Apr 24 '23 11:04 cmelchior