【发布时间】:2020-10-14 12:15:56
【问题描述】:
首先,我使用queryWaitingRoom() 查询 Firestore 中的一些文档,如果结果满足条件,则调用 runTransactionInWaitingRoom()。
这是第一个函数:
private fun queryWaitingRoom() {
waitingRoomRef
.orderBy("isAvailable", Query.Direction.DESCENDING)
.limit(1)
.get()
.addOnSuccessListener { documents ->
if (!documents.isEmpty){
for (document in documents) {
val waitingRoomId = document.get("id").toString()
runTransactionInWaitingRoom(waitingRoomId)
}
}
}
}
我的问题是,在runTransactionInWaitingRoom() 中,如果currentState 的值不等于true,我如何才能在else 块中停止transaction?并再次拨打queryWaitingRoom()?
这是第二个功能:
private fun runTransactionInWaitingRoom(waitingRoomId: String){
val docRef = waitingRoomRef.document(waitingRoomId)
db.runTransaction { transaction ->
val snapshot = transaction.get(docRef)
val currentState = snapshot.getBoolean("isAvailable")
if (currentState == true){
transaction.update(docRef, "isAvailable", false)
} else {
///// STOP THE TRANSACTION RIGHT HERE /////
///// And call the the queryWaitingRoom() again /////
Log.d("OIWENCLKSJF", "Transaction.currentState != true") // getting called 5 or 6 times.
throw FirebaseFirestoreException("Population too high", // only get called at the last attemp.
FirebaseFirestoreException.Code.ABORTED)
}
}.addOnSuccessListener {
}.addOnFailureListener { e ->
Log.w("OIWENCLKSJF", "Transaction failure.", e)
}
}
到目前为止,我得到的是 transaction 一直在调用自己,即使我将 exception 扔到 else 块中。
我在这里遇到了类似的问题,但没有帮助。如果有人能指导我一点,我将不胜感激。
【问题讨论】:
-
请编辑您的问题并将您的数据库结构添加为屏幕截图。除此之外,
isAvailable属性是否与id是同一文档的一部分? -
我不认为数据库结构与这个问题有任何关系。是的,
isAvailable属性来自同一个文档。同样isAvailable只能有两个值..true和false。首次创建时获取true的值。
标签: android kotlin google-cloud-firestore transactions