如何在 java 中轻松检索聊天应用程序的嵌套 firebase 实时数据库数据?

How to easily retrieve nested firebase realtime database data for a chat app in java?

我正在编写一个聊天应用程序,我需要在其中检查两个用户 u1u2 之前是否有过对话。如果有,则检索 conversationId 并将消息发送到该端点。否则,创建一个新对话。

我的数据结构看起来像这样。

userconversations:
    userId1:
        conversationID1:
            participants:
                user1: true
                user2: true
         conversationID2:
             participants:
                user1: true
                user7: true

在我的应用程序中,我想从 userconversations/userId1 读取所有数据并遍历列表以确定 user2 是否是对话的一部分。

我的问题是如何设计 class 以便快速反序​​列化此数据并避免使用 getChildren() 调用对其进行迭代?

如果我规范化数据并保持不同的 conversationId -> participants 映射,那么对于每个 conversationid,我将不得不查询该流以获取数据。假设一个用户可以进行 100 次对话,这将非常缓慢且占用大量资源。

请随时提出解决我的目的的替代架构设计。

由于键 user1user2user7 是动态的,它们不能自动序列化到 POJO 的属性中。您可以做的最好的事情是使用 GenericTypeIndicator 或强制转换从 participants 获得 Map<String, boolean>(Map<String, boolean>)snapshot.child("participants").getValue().