使用Kotlin将数据添加到Firebase实时数据库时,为什么会得到重复的值? [英] Why do i get duplicate values when adding data to firebase realtime database using kotlin?

查看:18
本文介绍了使用Kotlin将数据添加到Firebase实时数据库时,为什么会得到重复的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示用户发送到数据库的数据,但是每当将新数据添加到数据库时,它都会显示已存在于数据库中的数据的副本,如下所示:

Im trying to display data that a user send to the database but whenever new data is added to the database it displays duplicates of the data that was already in the database like this:

在添加新值之前:

在我添加"Apple"一词后,并将其发送到数据库,我看到以下内容:

After I add the word "Apple" and send it to the database i see the following:

如果我关闭应用程序并重新打开或刷新页面,我只会得到我想要的树条目:

And if i close the app and reopen or refresh the page i get only the tree entries which is what i want like this:

这是我的代码,用于将每个条目添加到数据库:

This is my code to add each entry to the database:

    val myRef = database.getReference("User-following").child(currentUid!!)


private fun addCategory() {
    val newCategory = category.text.toString().trim()
    val isValid = validateCategory(newCategory)

    if (isValid) {

        val exists = checkIfCategoryExists(newCategory)

        if (!exists) {
        dismissKeyBoard()
        categorysList.add(newCategory)
        category.text.clear()
        Toast.makeText(this,
            "$newCategory has been added to your list",
            Toast.LENGTH_LONG).show()
        sendDataToDatabase(newCategory)
            } else {
                category.error = "$newCategory has already been added"
            }
    }
}

这是我从数据库中提取数据的代码:

This is my code to pull the data from the database:

private fun fetchUsersFollowing() {


    myRef.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            for( childSnapshot in dataSnapshot.children) {
                if (!childSnapshot.hasChildren()) {
                    var test = childSnapshot.getValue()
                    val myCategory = CategoryModel()
                    myCategory.setCategories(test.toString())
                    list.add(myCategory)
                } else {
                    val noCategory = CategoryModel()
                    noCategory.setCategories("No Categories added.")
                    list.add(noCategory)
                    return
                }
            }
        }

        override fun onCancelled(error: DatabaseError) {
            Log.e("Database Error", error.toString())
        }
    })
}

推荐答案

传递给 onDataChange dataSnapshot 始终包含节点上存在的完整数据.由于您处理了整个快照,并将其添加到 list ,因此,随着时间的推移,您的列表最终将具有重复项,三次重复项以及更多内容.

The dataSnapshot that is passed to onDataChange always contains the complete data that exists at the node. Since you process that entire snapshot, and add it to list, your list will end up with duplicates, triplicates, and more over time.

最简单的解决方案是每次调用 onDataChange 时清除列表:

The simplest solution is to clear the list each time onDataChange is called:

myRef.addValueEventListener(object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        list.clear();
        for( childSnapshot in dataSnapshot.children) {
            ...
        }
    }

这篇关于使用Kotlin将数据添加到Firebase实时数据库时,为什么会得到重复的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆