【发布时间】:2021-04-09 18:27:17
【问题描述】:
我使用 firestore 作为我的后端数据库,保存数据如下所示:
suspend fun uploadDataToFirestore() {
val firestore = Firebase.firestore
var batch = firestore.batch
-- fill batch with data --
batch.commit().addOnCompleteListener {
if (it.isSuccessful) {
Timber.d("Successfully saved data")
doAdditionalSuspendingStuff()
} else {
Timber.d("error at saving data: ${it.exception}")
}
}
问题在于onCompleteListener,因为我无法调用其他挂起函数。有没有办法从 onCompleteListener 中调用挂起函数,但它们仍然附加到同一范围,因为我不希望函数 uploadDataToFirestore 在执行 doAdditionalSuspendingStuff() 之前完成。
【问题讨论】:
-
我认为你应该让 firebase 操作同步,比如 await()
标签: android kotlin kotlin-coroutines coroutinescope