Firebase 更新子项,通过替换先前的数据

huangapple go评论123阅读模式
英文:

Firebase updating the children by replacing previous data

问题

这是我的数据库结构

当我从服务器应用程序更新大小时,一些数据被覆盖了
以下是更新了库存1大小后Firebase结构的图像

以下是服务器应用程序在Firebase中更新大小或附加组件的代码

private void saveData() {
    if (foodEditPosition != -1) {
        Common.categorySelected.getFoods().set(foodEditPosition, Common.SelectedFood); //将食物保存到类别

        Map<String, Object> updateData = new HashMap<>();
        updateData.put("foods", Common.SelectedFood);

        FirebaseDatabase.getInstance()
                .getReference(Common.CATEGORY_REF)
                .child(Common.categorySelected.getMenu_id())
                .updateChildren(updateData)
                .addOnFailureListener(e -> {
                    Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
                })
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        Toast.makeText(this, "重新加载成功。", Toast.LENGTH_SHORT).show();
                        needSave = false;
                        edtPrice.setText("0");
                        edtName.setText("");
                    }
                });
    }
}
英文:

This is my database structure

Firebase 更新子项,通过替换先前的数据

when i updated the size from server app then some the data is overwritten
below is the image of Firebase structure after updating size of stock 1

Firebase 更新子项,通过替换先前的数据

Below is the code where the server app is updating the size or addon in firebase

private void saveData() {
    if (foodEditPosition != -1) {
        Common.categorySelected.getFoods().set(foodEditPosition, Common.SelectedFood); //Save Food to Category

        Map&lt;String, Object&gt; updateData = new HashMap&lt;&gt;();
        updateData.put(&quot;foods&quot;, Common.SelectedFood);

        FirebaseDatabase.getInstance()
                .getReference(Common.CATEGORY_REF)
                .child(Common.categorySelected.getMenu_id())
                .updateChildren(updateData)
                .addOnFailureListener(e -&gt; {
                    Toast.makeText(this, &quot;&quot; + e.getMessage(), Toast.LENGTH_SHORT).show();
                })
                .addOnCompleteListener(task -&gt; {
                    if (task.isSuccessful()) {
                        Toast.makeText(this, &quot;Reloaded Successfully.&quot;, Toast.LENGTH_SHORT).show();
                        needSave = false;
                        edtPrice.setText(&quot;0&quot;);
                        edtName.setText(&quot;&quot;);
                    }
                });
    }
}

答案1

得分: 1

我还没有测试过你的代码,但似乎你在使用 updateChildren() 方法,该方法会更新由映射键引用的路径上的子节点,请参阅文档。这里的 1、2、3 是 "foods" 的子节点,因此通过使用值 Common.SelectedFood 来更新它,1、2、3 将被删除。你可以同时执行 updateData.put("foods/1", Common.SelectedFood);,将其与你的映射一起使用,以更新位置为 1 的食物,或者移除 .updateChildren(updateData) 并使用 setValue 方法。

而映射选项用于同时更新,在这里使用 setValue 应该是可以的。希望能对你有所帮助。

英文:

i haven't tested your code but it seems like you're using updateChildren() which has the effect to update child node at the path referred by the keys of the map see the Documentation. here 1 , 2 , 3 are children of "foods" hence by updating it with the value Common.SelectedFood 1 , 2 , 3 are going to be deleted, you can either do updateData.put(&quot;foods/1&quot;, Common.SelectedFood); along with your map to update the food at 1 or remove .updateChildren(updateData) and use set value

and the map option is used for simultaneous update here setValue should be ok
hope it helps

huangapple
  • 本文由 发表于 2020年5月3日 19:49:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/61573931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定