Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Firebase(Android)中同时更新多个节点中的多个字段_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java 在Firebase(Android)中同时更新多个节点中的多个字段

Java 在Firebase(Android)中同时更新多个节点中的多个字段,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我尝试使用映射和更新子节点更新不同节点中的多个字段,但是firebase正在删除相应节点中的数据并添加数据。我希望数据得到更新,以前的数据保持不变。有趣的是,该逻辑在更新同一节点中的两个字段时起作用,但在引入多个节点时不起作用。请参阅下面的代码 我不想创建新字段,只想同时更新两个不同节点中的两个现有字段。每个节点有10个不同的字段,我想保留它们 我从viewholder.button(在回收器视图适配器中)内部调用此选项 String ref1=“users/”+currentUserId; S

我尝试使用映射和更新子节点更新不同节点中的多个字段,但是firebase正在删除相应节点中的数据并添加数据。我希望数据得到更新,以前的数据保持不变。有趣的是,该逻辑在更新同一节点中的两个字段时起作用,但在引入多个节点时不起作用。请参阅下面的代码


我不想创建新字段,只想同时更新两个不同节点中的两个现有字段。每个节点有10个不同的字段,我想保留它们

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

String ref1=“users/”+currentUserId;
String ref2=“user\u detail\u profile/”+currentUserId;
HashMap UpdateFBB1=新HashMap();
updateFbDb1.put(“姓名”、“阿尔伯特·爱因斯坦”);
更新fBDB1.put(“分数”,23);
HashMap UpdateFBB2=新HashMap();
更新fBDB2.put(“claps”,55);
更新fBDB2.put(“评论”,21);
HashMap updateFbDb3=新的HashMap();
updateFbDb3.put(参考文献1,updateFbDb1);
updateFbDb3.put(参考文献2,updateFbDb2);
fbdrefroot.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);
HashMap updateFbDb1=newhashmap();
updateFbDb1.put(“姓名”、“阿尔伯特·爱因斯坦”);
更新fBDB1.put(“分数”,23);
HashMap UpdateFBB2=新HashMap();
更新fBDB2.put(“claps”,55);
更新fBDB2.put(“评论”,21);
fbdrefroot.child(“用户”).child(currentUserId).updateChildren(updateFbDb1);
fbdrefroot.child(“用户详细信息配置文件”).child(currentUserId).updateChildren(updateFbDb2);

嘿,在向firebase节点添加数据时,您可以使用push方法

下面是创建实例的方法

    private lateinit var database: DatabaseReference
    // ...
    database = FirebaseDatabase.getInstance().reference

这就是简单地添加数据的方式

    mDatabase.child("users").child(userId).child("username").setValue(name);
这就是你推送数据的方式

    String key = mDatabase.child("posts").push().getKey();
    Post post = new Post(userId, username, title, body);
    Map<String, Object> postValues = post.toMap();

    Map<String, Object> childUpdates = new HashMap<>();
    childUpdates.put("/posts/" + key, postValues);
    childUpdates.put("/user-posts/" + userId + "/" + key, postValues);

    mDatabase.updateChildren(childUpdates);
String key=mDatabase.child(“posts”).push().getKey();
Post Post=新帖子(用户ID、用户名、标题、正文);
Map postValues=post.toMap();
Map childUpdates=newhashmap();
childUpdates.put(“/posts/”+键,postValues);
childUpdates.put(“/user posts/”+userId+“/”+key,postValues);
mDatabase.updateChildren(childUpdates);
另外,如果您想更新特定的文件

这里有一个链接

我尝试使用映射和更新子节点更新不同节点中的多个字段,但是firebase正在删除相应节点中的数据并添加数据

使用DatabaseReference时会发生这种情况:

将此位置的数据设置为给定值

展望未来,

我希望数据得到更新,以前的数据保持不变

在这种情况下,您应该使用DatabaseReference,我看到您已经在代码中使用了它

将特定子键更新为指定值

更进一步说,

这是工作,但我想在一个镜头这样一个成功的听众可以在所有或无基础上附加它

在这种情况下,您应该使用批处理操作,正如我在以下帖子中的回答所述:


现在可以向批处理操作添加完整侦听器或成功侦听器。还请注意,这是一个原子操作,这意味着要么所有操作都成功,要么没有应用任何操作。

以下解决方案基于@Alex Mamos suggestion正在运行

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);
Map Map=newhashmap();
map.put(“/users/”+currentUserId+“/name/”,“Albert Einstein”);
map.put(“/users/”+currentUserId+“/score/”,23);
map.put(“/user\u detail\u profile/”+currentUserId+“/claps/”,45);
map.put(“/user\u detail\u profile/”+currentUserId+“/comments/”,8);
fbdrefroot.updateChildren(map);

不知何故,在地图中插入地图不起作用。所有这些都必须是大型地图的一部分。

您使用的是实时数据库或firestore?实时数据库很好,让我来帮您回答。你应该使用firebase push方法来添加新数据这不是新数据,只是更新一些字段。对了,我想你想要新的行。我不是在尝试创建新字段,只是同时更新两个不同节点中的两个现有字段,我可以使用嵌套更新来完成,但这是一个很长的过程…嗨,Alex,我很感谢你花了这么多时间来给出答案,但是有没有可能给出一些工作代码来说明什么是有效的呢。理论上,我问题中的第一个代码应该可以工作,但它不工作。请参阅我的答案。当所有节点都作为单个大地图输入到地图中时,而不是地图中的地图时,更新子项似乎可以工作。。。太可怕了,我不能为你编码。一般来说,根据答案中的信息,你应该自己尝试,如果出现其他问题,你应该问另一个问题。除此之外,您的答案与我上面的答案中链接的解决方案完全相同,我特别添加了它以帮助您。所以你所做的,就是复制我的答案并把它放在你的答案里。希望你能做对:)嗨,亚历克斯,你的答案很相似,但不准确。我的案例涉及每个节点中的两个字段。每个节点中只有一个字段。其次,我尝试在地图中添加地图。我的回答将证明这是行不通的,正确的方法是制作一张大地图。这将在某种程度上帮助许多其他用户,而您的答案不会。我可以干脆不发布任何东西,因为我自己的答案是正确的,所以我得不到任何分数。我们的目标是在收到这么多的回复后,将一些东西返还给stackoverflow。PS我在下面的回答中感谢您,感谢您对我的帮助。现在可以添加一个成功的侦听器,它将作为一个批更新。这对我来说很有用。多亏了亚历克斯和龙火
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);