android - FireBase 按文档和集合进行多个查询
问题描述
我正在努力使用 firebase 运行一个查询以获取 truckDocumentId,然后运行另一个查询以获取 routesByDateDocumentId,最后我使用两个文档 ID 来运行函数“sendGpsPosition”,我的问题是第一个查询找到了 truckDocumentId但有时第二个查询不会执行,这就是应用程序停止的原因。下面的代码适用于 Kotlin。
如果我在调试,那么大部分时间都可以工作..如果我关闭调试,它几乎会显示下面的错误 =>
由于查询没有执行,我得到了这个错误:java.lang.IllegalArgumentException: Invalid document reference。文档引用必须有偶数个段,但卡车有 1
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)
}
}
推荐阅读
- mongodb - 连接来自 2 个模型的数据的最佳实践
- github - 如何基于多个工作流触发 GitHub Action 工作流
- sql-server - 使用 pivot 子句时用 0 替换 NULL
- html - 阻止 Blazor WebAssembly 在按 Enter 时提交表单
- c++ - 表示动态类成员变量类型的最佳方法?
- sql - 使用 R DBI 包获取 SQL 表的完整“路径”
- python - 如果文件夹不存在,则引发异常 pathlib python
- html - Placing an Image inside another png/svg file
- python-3.x - Open3D 点云的 Draco 压缩
- c# - 在 WPF 中获取 Windows 10 文本缩放值