如何在firebase中的一个请求中删除多个节点?

mad*_*dim 11 android firebase firebase-realtime-database

我有一个根的两个节点,我想在一个请求中删除它们中的数据.两个子节点具有相同的密钥.我试过这个:

Firebase firebaseRef = new Firebase(<root_path>);

Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put(<path_to_first_node>, key);
childUpdates.put(<path_to_second_node>, key);

listToRemoveRef.updateChildren(childUpdates, null);
Run Code Online (Sandbox Code Playgroud)

但它仅从第一个节点删除了数据

Den*_*und 3

看起来您使用的updateChildren函数是错误的。你想做的是这个

Firebase firebaseRef = new Firebase(<root_path>);

Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("<path_to_first_node>" + key, null);
childUpdates.put("<path_to_second_node>" + key, null);

listToRemoveRef.updateChildren(childUpdates);
Run Code Online (Sandbox Code Playgroud)

第二个参数 toupdateChildren不设置值,null它是一个可选的完成侦听器(请参阅文档)。因此,null您可以省略它,而不是在最后一行传递它。