android - Firebase 覆盖单个文档问题
问题描述
我正在尝试覆盖 Kotlin 中的单个文档。对于创建文档,用户给出了自动创建的目标分数和分数(“分数”:0),然后每次点击图像,数字都会增加,但问题是;如果目标分数例如 10,当我点击图像分数时,结果将直接变为 10(目标分数)而不是 1。
这是我的代码。谢谢!
fun pointClicked(view: View) {
db.collection(auth.currentUser!!.email.toString()).document(documentId!!).collection("tasks").whereEqualTo("taskDocId", taskDocumentId).addSnapshotListener { snapshot, exception ->
if (exception != null) {
Toast.makeText(applicationContext,exception.localizedMessage.toString(),Toast.LENGTH_LONG).show()
} else {
if (pointTextView.text.toString().toInt() == targetPointTextView.text.toString().toInt()) {
println("Task completed")
} else if (pointTextView.text.toString().toInt() < targetPointTextView.text.toString().toInt()) {
var increasePoint = pointTextView.text.toString().toInt() + 1
val setPoint = hashMapOf("point" to increasePoint)
db.collection(auth.currentUser!!.email.toString()).document(documentId!!).collection("tasks").document(taskDocumentId!!).set(setPoint, SetOptions.merge())
println("Task is not completed")
}
}
}
}
编辑:当我点击 imageView 一次点必须增加 +1 时,例如 Point 值为 0(“point”:0),当我点击 imageView 一次时,Point 值必须为 1(“point”:1)。但它不会增加 +1,点会增加,直到值与 targetPoint 值相等。
这是新代码:
fun pointClicked(view: View) {
db.collection(auth.currentUser!!.email.toString()).document(documentId!!).collection("tasks").whereEqualTo("taskDocId", taskDocumentId).addSnapshotListener { snapshot, exception ->
if (exception != null) {
Toast.makeText(applicationContext,exception.localizedMessage.toString(),Toast.LENGTH_LONG).show()
} else {
if (snapshot != null) {
if (!snapshot.isEmpty) {
val documents = snapshot.documents
for (document in documents) {
val point = document.get("point") as Long
val targetPoint = document.get("targetPoint") as Long
pointTextView.text = point.toString()
targetPointTextView.text = targetPoint.toString()
if (pointTextView.text.toString().toInt() == targetPointTextView.text.toString().toInt()) {
println("Task completed")
} else if (pointTextView.text.toString().toInt() < targetPointTextView.text.toString().toInt()) {
var increasePoint = pointTextView.text.toString().toInt() + 1
val setPoint = hashMapOf("point" to increasePoint)
db.collection(auth.currentUser!!.email.toString()).document(documentId!!).collection("tasks").document(taskDocumentId!!).set(setPoint, SetOptions.merge())
println("Task is not completed")
}
}
}
}
}
}
}
解决方案
你正在做的是导致一个循环(见内联注释):
db.collection(auth.currentUser!!.email.toString()).document(documentId!!).collection("tasks").whereEqualTo("taskDocId", taskDocumentId).addSnapshotListener { snapshot, exception ->
//
// CODE HERE WILL RUN EVERY TIME the "tasks" document is updated
//
if (exception != null) {
// ...
} else {
if (snapshot != null) {
if (!snapshot.isEmpty) {
val documents = snapshot.documents
for (document in documents) {
// ...
if (...) {
// ...
} else if (...) {
var increasePoint = pointTextView.text.toString().toInt() + 1
val setPoint = hashMapOf("point" to increasePoint)
//
// THIS CODE UPDATES THE SAME DOCUMENT, CAUSING THE SNAPSHOT LISTENER
// TO FIRE AGAIN
//
db.collection(auth.currentUser!!.email.toString()).document(documentId!!).collection("tasks").document(taskDocumentId!!).set(setPoint, SetOptions.merge())
println("Task is not completed")
}
}
}
}
}
}
您正在更新其自己的更改侦听器中的文档。这会导致更改侦听器再次触发,从而导致另一次更新。重复此过程,直到达到极限条件,在本例中为 score=10。
目前尚不清楚您为什么要这样做,但我建议您将代码的侦听器和分数更新部分分开。
推荐阅读
- apache-flink - Flink写入空文件
- linux - 命令 'bg' 和 '&' 在 Linux 终端上不起作用
- jmeter - 如何使用 Jmeter 加载测试 GraphQL API
- java - JTable 如何在双列类中将数字显示为 (#.00) 格式?
- c++ - VS2019 最新更新:constexpr:FIXED 字符串数组的“表达式未计算为常量”
- c# - C#中不同输出类型的动态参数列表
- android - 未找到构建到 Android 应用程序模块的 Buildozer
- postgresql - Spark 优化 - 加入 - 任务数量非常少 - OOM
- sql - Postgresql中的子查询与预期的数据不匹配
- python - NEST 内核如何完全重置?