Firebase - 如何一次删除多个条目? [英] Firebase - How to delete many entries at once?

查看:34
本文介绍了Firebase - 如何一次删除多个条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除给定推送 ID 的所有条目?

How do I delete all entries for the given push ID?

例如,假设 KoxoxwTqfb50E1Gvi9F 推送 ID 位于我数据库的许多位置,即在许多键下,我想一次删除 KoxoxwTqfb50E1Gvi9F 的所有条目,而不是静态删除所有条目(因为我知道它们的位置).

For example, let's say KoxoxwTqfb50E1Gvi9F push ID is in many locations of my database i.e. under many keys and I want to delete all entries for KoxoxwTqfb50E1Gvi9F at once as opposed to statically deleting all entries (since I know their locations).

换句话说,有没有办法告诉 Firebase删除整个数据库中 KoxoxwTqfb50E1Gvi9F 的所有条目"?

In other words, is there a way to tell Firebase "delete all entries for KoxoxwTqfb50E1Gvi9F across the entire database"?

推荐答案

为了从数据库中删除多个条目,您需要知道所有这些位置(引用).所以换句话说,在你添加数据的方式中,你也应该删除它.

In order to delete multiple entries from your database, you need to know all those locations (refernces). So with other words, in the way you add data, you should also delete it.

假设您的数据库如下所示:

Assuming your database looks like this:

Firebase-root
   |
   --- Users
   |     |
   |     --- userUid1
   |     |      |
   |     |      --- //user1 data
   |     |
   |     --- userUid2
   |            |
   |            --- //user2 data
   |
   --- Groups
         |
         --- groupId1
         |      |
         |      --- //group1 data
         |      |
         |      --- Users
         |            |
         |            --- userUid1: true
         |            |
         |            --- userUid3: true
         |
         --- groupId2
                |
                --- //group2 data

建议您使用以下方法:

private static void deleteUser(String userId, String groupId) {
    Map<String, Object> map = new HashMap<>();
    map.put("/Users/" + userId + "/", null);
    map.put("/Groups/" + groupId + "/Users/" + userId + "/", new HashMap<>().put(userId, null));
    //other locations
    databaseReference.updateChildren(map);
}

此方法自动删除所有这些条目.使用这些路径,您可以通过一次调用 deleteUser() 方法对 JSON 树中的多个位置执行同时更新.以这种方式同时删除是原子的:要么所有更新成功,要么所有更新失败.

This method atomically deletes all those entries. Using these paths, you can perform simultaneous updates to multiple locations in the JSON tree with a single call to deleteUser() method. Simultaneous deletes made this way are atomic: either all updates succeed or all updates fail.

希望有帮助.

这篇关于Firebase - 如何一次删除多个条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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