聊天应用程序的实时数据库结构

时间:2018-09-21 18:27:57

标签: android firebase firebase-realtime-database chat

我正在使用Firebase Realtime DB和云功能开发聊天应用程序。关于数据库的结构以及如何检索Chat所属的User节点,我遇到了问题。我的数据库具有以下结构

{
  "chats" : {
    "chatId1" : {
      "lastMessage" : "example of last message",
      "timestamp" : "11111"
    },
    "chatId2" : {
      "lastMessage" : "generic last message",
      "timestamp" : "22222"
    }
  },

  "members" : {
    "chatId1" : {
      "uid1" : true,
      "uid2" : true
    },
    "chatId2" : {
      "uid1" : true,
      "uid3" : true
    }
  },

  "users" : {
    "uid1" : {
      "name" : "UserName1"
    },
    "uid2" : {
      "name" : "UserName2"
    },
    "uid3" : {
      "name" : "UserName3"
    }
  }
}

目前,我已经找到了以下解决方案:https://stackoverflow.com/a/37579607/7403707,这建议在每个users子下添加一个/chats子节点并执行深度查询。但是在阅读了文档并观看了一些视频之后,我发现这并不是最佳选择,因为它会下载整棵树。我想到的另一件事是创建一个名为/userChats的单独节点,该节点具有以下结构:

{
  "userChats" : {
    "uid1" : {
      "chatId1" : true,
      "chatId2" : true
    },
    "uid2" : {
      "chatId1" : true
    },
    "uid3" : {
      "chatId3" : true
    }
  }
}

并执行以下操作:

  1. /userChats获取特定用户(例如,具有id = uid1 的用户)的所有键( chatIds )。
  2. 对结果快照的每个键(chatId)执行另一个提取,这将是另一个快照,其值为Chat类型。
  3. 将所有提取的Chat对象存储在Array中。
  4. 将此数组传递给我的应用的recyclerView适配器。

您有什么建议?希望,如果可能的话,我希望使其与Firebase RecyclerView一起使用。谢谢。

0 个答案:

没有答案