Dra*_*ire 3 java android firebase firebase-realtime-database
我正在尝试使用 Maps 和 Update children 更新不同节点中的多个字段,但是 firebase 正在删除相应节点中的数据并添加数据。我希望数据得到更新并且以前的数据保持不变。有趣的是,该逻辑在更新同一节点中的 2 个字段时有效,但在引入多个节点时无效。请看下面的代码。
我不是要创建新字段,而只是同时更新 2 个不同节点中的 2 个现有字段。每个节点都有 10 个不同的字段,我想保留它们。
我从 viewholder.button 内部调用它(在回收器视图适配器中)
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);
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我想一次性完成,以便可以完全或不附加成功的听众。
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);
Run Code Online (Sandbox Code Playgroud)
以下解决方案基于@Alex Mamos 的建议有效...
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);
Run Code Online (Sandbox Code Playgroud)
不知何故在地图中插入地图不起作用。这一切都必须是大地图的一部分。
| 归档时间: |
|
| 查看次数: |
1629 次 |
| 最近记录: |