gpt4 book ai didi

firebase - 为什么在 Kotlin 中迭代 firebase 数据库时元素值添加了两次?

转载 作者:行者123 更新时间:2023-12-02 13:38:31 26 4
gpt4 key购买 nike

我正在尝试迭代 firebase-database并将其元素添加到我的对象中。它可以工作,但由于某种原因我无法理解,它将每个元素广告两次,日志输出显示如下:

TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Tahini-Oat Cookies } 
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Tahini-Oat Cookies }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Chocolate & Cinnamon Cookies }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Chocolate & Cinnamon Cookies }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Sweet Potato Muffin }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Sweet Potato Muffin }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Almond Butter Cookies }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Almond Butter Cookies }
Firebase-database结构看起来像这样(请注意,我现在忽略了其他元素):

FbDb

我的 Kotlin 代码如下所示:
fbdb = FirebaseDatabase.getInstance()
ref = fbdb!!.getReference("cookies")

ref!!.addChildEventListener(object: ChildEventListener {

override fun onChildAdded(snapshot: DataSnapshot?, p1: String?) {
val children = snapshot!!.children //DIRECT REFERENCE TO CHILD, USED FOR LOOP
val header = snapshot!!.child("recipeHeaderFirebase")

for(it in children) {
var tempRecipe = RecipeTemplate()
tempRecipe.recipeHeader = header.toString()
Log.d("TAGZ", tempRecipe.recipeHeader) //OUTPUT SHOWS DUPLICATE VALUES
}
}
})

我错过了什么?我使用类似的代码成功检索了 Xcode 中的元素...

编辑 应要求提供完整代码:
package com.healthandchocolate.sjostedtafzelius.healthchocolateandroid

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.google.firebase.database.*

class RecipeGridView : AppCompatActivity() {

private var fbdb: FirebaseDatabase? = null
private var ref: DatabaseReference? = null
var recipeArray: MutableList<RecipeTemplate> = mutableListOf()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recipe_grid_view)
Log.d("TAG", "ON CREATE");

fbdb = FirebaseDatabase.getInstance()
ref = fbdb!!.getReference("cookies")

ref!!.addChildEventListener(object: ChildEventListener {

override fun onChildChanged(snapshot: DataSnapshot?, p1: String?) {

}

override fun onChildMoved(p0: DataSnapshot?, p1: String?) {
Log.d("TAG", "ON CHILD MOVED");
}

override fun onChildRemoved(p0: DataSnapshot?) {
Log.d("TAG", "ON CHILD REMOVED");

}

override fun onCancelled(error: DatabaseError) {
print(error)
Log.d("TAG", "ON ERROR");

}

override fun onChildAdded(snapshot: DataSnapshot?, p1: String?) {
Log.d("TAGZ", "CALLED") //keyName of each child

val children = snapshot!!.children //DIRECT REFERENCE TO CHILD, USED FOR LOOP
val header = snapshot!!.child("recipeHeaderFirebase")

for(it in children) {
var tempRecipe = RecipeTemplate()
tempRecipe.recipeHeader = header.toString()
//Log.d("TAGZ", tempRecipe.recipeHeader) //keyName of each child

recipeArray.add(tempRecipe)
}
}
}) //END FB CODE
}
}

最佳答案

每次系统将意图路由到当前事件的实例时,都会创建事件。所以函数onCreate可以多次调用。

每次调用您的函数时,您为 addChildEventListener 保存的回调函数事件被添加到监听器。此后,当事件被触发时,回调函数会被调用,就像它之前链接到监听器一样多。

为了防止这种行为,您可以定义 android:launchmode:"singleTop"在您的应用 list 文件中的标签 <activity>

这允许系统将意图路由到其他函数 onNewIntent而不是 onCreate
欲了解更多信息,请浏览:
https://developer.android.com/guide/topics/manifest/activity-element#lmode

我希望这将有所帮助。

我尽力写正确的英语,所以请随时纠正我。

关于firebase - 为什么在 Kotlin 中迭代 firebase 数据库时元素值添加了两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50405727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com