【发布时间】:2021-12-08 11:27:39
【问题描述】:
我正在尝试在 Kotlin 中的挂起函数下运行 Firebase 事务,但我没有看到有关它的文档。
我正在使用
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.5.2'
对于带有 firebase 的协程(例如: setValue(*).await() ),但 runTransaction(*)
似乎没有 await 功能override suspend fun modifyProductStock(
product: ProductModel,
valueToModify: Long,
replace: Boolean
) {
CoroutineScope(Dispatchers.Main).launch {
val restaurantId = authRepository.restaurantId.value ?: throw Exception("No restaurant!")
val productId = product.id ?: throw Exception("No Product ID!")
val reference = FirebaseDatabase.getInstance().getReference("database/$restaurantId").child("products")
if (replace) {
reference.child(productId).child("stock").setValue(valueToModify).await()
} else {
reference.child(productId).child("stock")
.runTransaction(object : Transaction.Handler {
override fun doTransaction(p0: MutableData): Transaction.Result {
//any operation
return Transaction.success(p0)
}
override fun onComplete(p0: DatabaseError?, p1: Boolean, p2: DataSnapshot?) {
}
})
}
}
}
【问题讨论】:
标签: firebase kotlin firebase-realtime-database kotlin-coroutines