JSON构建群聊

时间:2016-04-13 17:31:02

标签: json firebase

我将使用Firebase在我的应用中实施群聊。我在考虑使用JSON保存数据的两种不同结构。

第一个结构: Picture of my idea of a structure 第二种结构: Picture of my second structure

我想实现快速查询,我希望每次都能解析少量数据。我应该采用什么样的结构,是否有更好的替代方案呢?

1 个答案:

答案 0 :(得分:5)

第一个解决方案显然不可行,因为您很难找到属于给定组的所有消息。

第二种解决方案是正常的,如果每次您将查询给定的组的节点,您还需要所有的消息,这是可能不是你想要的。

当然,很难就数据结构提供建议而没有关于您的用例,您要进行的查询等的更多信息,但是相当标准的方法是:

{
    "users": {
        "$userId"": {
            // user data
        }
    },
    "groups": {
        "$groupId": {
            // group data
        }
    },
    "group_users": {
        "$groupId": {
            "$userId": true
            // separation of list of users from the group is useful
            // if you are going to query the group node not needing its full list of users
        }
    },
    "group_messages": {
        "$groupId": {
            "$messageId": {
                // message data
            }
        }
    }
}
相关问题