在Firebase(Android)中同时更新多个节点中的多个字段 [英] Update Multiple Fields in Multiple Nodes Simultaneously in Firebase (Android)

查看:83
本文介绍了在Firebase(Android)中同时更新多个节点中的多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用地图"和更新子项"更新不同节点中的多个字段,但是firebase正在删除各个节点中的数据并添加数据.我希望更新数据,并且以前的数据保持不变.有趣的是,该逻辑在更新同一节点中的2个字段时有效,但在引入多个节点时无效.请参见下面的代码.

I am trying to update multiple fields in different nodes using Maps and Update children however firebase is deleting the data in the respective nodes and adding the data. I want the data to be updated and previous data to remain the same. Interestingly the logic works while updating 2 fields in the same node, but not when multiple nodes are introduced. Please see the code below.

我不是要创建新字段,而只是同时更新2个不同节点中的2个现有字段.每个节点都有10个我要保留的不同字段.

I am not trying to create new fields, but merely update 2 existing fields each in 2 different nodes simultaneously. Each nodes has 10 different fields which I want to keep.

我是从viewholder.button内部(在回收器视图适配器中)调用此

I am calling this from inside a viewholder.button (in a recycler view adapter)

String ref1 = "users/" + currentUserId;
String ref2 = "user_detail_profile/" + currentUserId;

HashMap<String, Object> updateFbDb1 = new HashMap<>();
updateFbDb1.put("name", "Albert Einstein");
updateFbDb1.put("score", 23);

HashMap<String, Object> updateFbDb2 = new HashMap<>();
updateFbDb2.put("claps", 55);
updateFbDb2.put("comments", 21);

HashMap<String, Object> updateFbDb3 = new HashMap<>();

updateFbDb3.put(ref1, updateFbDb1);
updateFbDb3.put(ref2, updateFbDb2);

fbDbRefRoot.updateChildren(updateFbDb3);

这是可行的,但我想一次完成,这样就可以完全或不附加成功的侦听器.

HashMap<String, Object> updateFbDb1 = new HashMap<>();
updateFbDb1.put("name", "Albert Einstein");
updateFbDb1.put("score", 23);

HashMap<String, Object> updateFbDb2 = new HashMap<>();
updateFbDb2.put("claps", 55);
updateFbDb2.put("comments", 21);

fbDbRefRoot.child("users").child(currentUserId).updateChildren(updateFbDb1);
fbDbRefRoot.child("user_detail_profile").child(currentUserId).updateChildren(updateFbDb2);

推荐答案

以下解决方案基于@Alex Mamos的建议正在起作用...

The following solution is working based on @Alex Mamos suggestion is working...

Map<String, Object> map = new HashMap<>();
map.put("/users/" + currentUserId + "/name/", "Albert Einstein");
map.put("/users/" + currentUserId + "/score/", 23);
map.put("/user_detail_profile/" + currentUserId + "/claps/", 45);
map.put("/user_detail_profile/" + currentUserId + "/comments/", 8);
fbDbRefRoot.updateChildren(map);

以某种方式无法将地图插入地图.这都必须是大地图的一部分.

Somehow inserting maps inside maps does not work. It all has to be a part of a large map.

这篇关于在Firebase(Android)中同时更新多个节点中的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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