【问题标题】:How to delete multiple nodes in one request in firebase?如何在firebase的一个请求中删除多个节点?
【发布时间】:2016-05-31 18:33:43
【问题描述】:

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

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);

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

【问题讨论】:

  • 当我重新编译项目时,上面的代码工作了。
  • 将 null 传递给 childUpdates.put(, null);似乎是更合适的答案

标签: android firebase firebase-realtime-database


【解决方案1】:

您似乎使用了错误的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);

updateChildren 的第二个参数没有将值设置为 null,它是一个可选的完成侦听器 (see documentation)。因此,不要在最后一行将null 传递给它,您可以省略它。

【讨论】:

  • 这个问题是三年前的问题,所以到那时它可能已经改变了:)
  • 不,实际上你当时和现在对updateChildren 的使用都是错误的——the second parameter is and was for "completion callback"。您问题中的代码表明您希望使用第二个参数将值设置为 null ,因为您在更新映射中传递的是键而不是值。因此,如果您认为您的问题已过时,则应将其删除,否则您可能会发现此答案很有用。
猜你喜欢
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多