GitLiveApp / firebase-kotlin-sdk

A Kotlin-first SDK for Firebase
https://gitliveapp.github.io/firebase-kotlin-sdk/
Apache License 2.0
1.17k stars 155 forks source link

Query.where with inArray not working properly? #420

Closed DavinRot closed 7 months ago

DavinRot commented 1 year ago

So first off, my assumption about the Query.where method that takes params field: String, inArray: List<Any> is that it should be returning a list of all entries from that collection where the field matches an entry in that list (in my case the field is an id and I'm checking against a list of Strings). First off, please correct me if I'm just using the query wrong. Assuming I'm not, I can't get it to work - I keep getting an empty list, but using Query.where with field: String, equalTo: Any? and piecing the list together works fine. Is the method bugged, or am I just not using it right?

Qw4z1 commented 1 year ago

It's definitely broken.

Tried the functions below. The common version doesn't work (returns zero results), but the ones where I use the native versions work as expected.

Tried looking at the source code, but as far as I can tell it should work.

I am using expect/actual as a workaround for now.

// common - Doesn't work (returns zero results)
actual suspend fun query(categories: List<String>): Query =
    Firebase.firestore.collection("categories")
        .where("name", inArray = categories)

// Android - works (returns seven results as expected)
actual suspend fun query(categories: List<String>): Query = Query(
    Firebase.firestore.collection("categories").android
        .whereIn("name", categories)
)

// iOS - works (returns seven results as expected)
actual suspend fun query(categories: List<String>): Query = Query(
    Firebase.firestore.collection("categories")
        .ios.queryWhereFieldPath(FIRFieldPath(listOf("name")), `in` = categories))
andjela-aetherius commented 11 months ago

It looks like method fun Query.where(field: String, inArray: List<Any>? = null, arrayContainsAny: List<Any>? = null) internally calls method internal fun _where(field: String, lessThan: Any? = null, greaterThan: Any? = null, arrayContains: Any? = null): Query instead of method internal fun _where(field: String, inArray: List<Any>? = null, arrayContainsAny: List<Any>? = null): Query. Internal method that uses array is not even being used.

nbransby commented 7 months ago

@Daeda88 has your changes to where clauses fixed this issue now?

Daeda88 commented 7 months ago

According to this test it should work https://github.com/GitLiveApp/firebase-kotlin-sdk/blob/2e736d437c19cd27a16da2f208e6338db66e13c8/firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt#L954

andjela-aetherius commented 7 months ago

Yeah, now it works, thank you