【问题标题】:Best way to delete Firestore document that you are unsure if it exists?删除不确定是否存在的 Firestore 文档的最佳方法?
【发布时间】:2020-06-08 13:12:36
【问题描述】:

我正在使用 Firestore 作为 NoSQL 数据库来构建一个需要让用户添加为朋友、阻止...其他用户的应用

要屏蔽某人,我会设置屏蔽值,然后删除当前的友谊状态(如果有的话),但我觉得这有点棘手。我应该先检查文档是否存在,然后再将其删除,还是 Firestore 会自动实现这一点?如果我添加额外的检查,我会浪费时间和 Firestore 操作吗?

fbRef.runBatch { 
    it.delete(userFriendsWith)
    it.delete(blockedUserFriendsWith)
    ...
}

fbRef.runBatch { 
    it.get() {
        ...
        if (document.exists()) {
            it.delete(userFriendsWith)
        }
    }
}

谢谢!

【问题讨论】:

    标签: kotlin google-cloud-firestore


    【解决方案1】:

    只需删除文档。如果您不在乎里面有什么,则无需先阅读它。如果文档不存在,删除操作不会失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多