英文:
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
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
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<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, "Reloaded Successfully.", Toast.LENGTH_SHORT).show();
needSave = false;
edtPrice.setText("0");
edtName.setText("");
}
});
}
}
答案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("foods/1", 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论