Android FireBase按文档和集合的多个查询

Android FireBase按文档和集合的多个查询,android,firebase,kotlin,google-cloud-firestore,Android,Firebase,Kotlin,Google Cloud Firestore,我正在努力与firebase一起运行一个查询以获取truckDocumentId,然后运行另一个查询以获取RoutesByDateDocumentId,最后我使用两个文档ID来运行函数“sendGpsPosition”,我的问题是,第一个查询查找truckDocumentId,但有时第二个查询不执行,这就是应用程序停止的原因。下面的代码是针对Kotlin的 如果我正在调试,那么大部分时间都能正常工作。。如果我关闭调试,它几乎会显示以下错误=> 由于查询没有执行,我得到了以下错误:java.lan

我正在努力与firebase一起运行一个查询以获取truckDocumentId,然后运行另一个查询以获取RoutesByDateDocumentId,最后我使用两个文档ID来运行函数“sendGpsPosition”,我的问题是,第一个查询查找truckDocumentId,但有时第二个查询不执行,这就是应用程序停止的原因。下面的代码是针对Kotlin的

如果我正在调试,那么大部分时间都能正常工作。。如果我关闭调试,它几乎会显示以下错误=>

由于查询没有执行,我得到了以下错误:java.lang.IllegalArgumentException:无效的文档引用。文档引用的段数必须为偶数,但卡车的段数为1


与其显示我们看不到其值的变量,不如硬编码所有文档和集合字符串,并显示这些代码。或者,向我们显示导致此错误的所有变量的值。我建议阅读:问题是有时我在第一个查询中找到documentId,但第二个查询没有找到它。。。然后它崩溃了。。如果我调试它是确定的,我需要做它“异步”以某种方式与协程。。。但是我不能让它工作我更改了代码但仍然不工作现在检查,我更新了代码我们仍然看不到您在查询中使用的变量的值。在使用它们之前,您应该记录它们,以确保它们符合您的预期。默认值为:var routesByDateDocumentId=“”var truckDocumentId=“”,因此,如果我没有从firebase获得任何信息,我会将其放入“”,因为这会导致崩溃。。不知何故,该流程不会等待1流程完成,抱歉,Android Kotlin的第一天我的老板让我去修理一些东西,但这和我以前的工作不太一样
suspend fun getTruckId() {
    val trucksReference = firestore.collection("trucks").whereEqualTo("dispatcher", "Miro")
        .whereEqualTo("name", "PEUGEOT").get().await()
    val document = trucksReference.documents[0]
    if (document != null) {
        truckDocumentId = document.id
    }
}

suspend fun getRouteReferenceId() {
    val routesByDate = firestore.collection("trucks")
        .document(truckDocumentId)
        .collection("routes_by_date").get().await()
    val documentRoute = routesByDate.documents[0]
    if (documentRoute != null) {
        routesByDateDocumentId = documentRoute.id
    }
}


fun sendGpsPosition(lat: Double, long: Double, imageRef: String? = null) {
    runBlocking { getTruckId() } // if I get this DocumentID
    runBlocking { getRouteReferenceId() } // this here maybe will be not found or maybe will be found.. the async is not done correct not sure how to do it.
    firestore
        .collection("trucks")
        .document(truckDocumentId)
        .collection("routes_by_date")
        .document(routesByDateDocumentId)
        .collection("live_route")
        .add(LatLong(Timestamp.now(), lat, long))
}
 **I solved it this way.**

private suspend fun getTruckId() {
            val trucksReference = firestore.collection("trucks")
                .whereEqualTo("dispatcher", "Miro")
                .whereEqualTo("name", "VW")
                .get()
                .await()
            val document = trucksReference.documents[0]
            if (document != null) {
                truckDocumentId = document.id
            }
        }
    
        private suspend fun getRouteReferenceId() {
            val currentTime = Timestamp.now()
            val routesByDate = firestore.collection("trucks")
                .document(truckDocumentId)
                .collection("routes_by_date")
                .get()
                .await() // here will be better to look for data by delivery_day
            val documentRoute = routesByDate.documents[0]
            if (documentRoute != null) {
                routesByDateDocumentId = documentRoute.documents[0].id
            }
        }
    
        private fun addGpsDataInDatabase(lat: Double, long: Double, imageRef: String? = null) {
            firestore
                .collection("trucks")
                .document(truckDocumentId)
                .collection("routes_by_date")
                .document(routesByDateDocumentId)
                .collection("planned_route")  //planned_route or live_route depends if we want to show current state of a truck of make a route plan
                .add(LatLong(Timestamp.now(), lat, long))
        }
    
        fun sendGpsPosition(lat: Double, long: Double, imageRef: String? = null) {
            GlobalScope.launch {
                val truckDocId = async { getTruckId() }
                truckDocId.await()
                val routeDocId = async { getRouteReferenceId() }
                routeDocId.await()
                addGpsDataInDatabase(lat, long, imageRef)
            }
    
        }