gpt4 book ai didi

android - Firebase 数据扁平化

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:38 25 4
gpt4 key购买 nike

我有一个关于 Firebase 数据库中数据结构最佳实践的快速问题。

我希望我的应用程序的用户能够维护好友列表。 firebase 文档建议创建一个尽可能扁平的模式(不确定在这种情况下这是否是正确的词)。因此,我认为将数据库中的 friend 部分与玩家部分分开是个好主意,如下所示:

{


"players":{
"player1id":{
"username":"john",...
},
"player2id": ...,
"player3id": ...
}


"friends": {
"player1id"{
"friends":{
"friend1Id":true,
"friend2Id":true
}
},
}
"player2id"{
"friends":{
"friend1Id":true,
"friend2Id":true
}
},
}
}

所以我的问题如下:

这对我的架构来说是一个好的设计吗?

拉取一个玩家的好友列表时,会拉取所有玩家的好友列表吗?如果是这样,可以避免这种情况吗?

此外,一旦应用程序拥有 friend 的所有 ID,最好的方法是什么才能提取有关 friend 的其他信息。例如获取他们所有的用户名,这些用户名将作为字符串存储在他们的玩家资料中。

最佳答案

Is this a good design for my schema?

您的思考方向已经正确。然而,“ friend ”节点可以简化为:

 "friends": {
"player1id": {
"friend1Id":true,
"friend2Id":true
}
}

请记住,Firebase 节点名称不能使用字符点 (.)。因此,如果您的 ID 是整数,例如 1、2 和 3,一​​切都可以,但如果 ID 是用户名,请小心(例如“super123”可以,但“super.duper”则不行)

When pulling a friends list for one player, will the friends lists of EVERY player be pulled? and if so, can this be avoided?

没有。如果你拉/friends/1 它显然不会拉/friends/2 等。

Also, what would be the best way to then pull in additional information about the friends once the app has all of their IDs. e.g. getting all of their user names which will be stored as a string in their player profile.

遍历 ID 并再次从 Firebase 获取相应的节点。例如,如果用户 1 有 friend 2、3 和 4,则使用 for 循环获取/players/2、/players/3 和/players/4

由于 firebase pull 异步工作,您可能需要使用计数器或其他一些机制,以便在拉取最后一个数据时可以继续运行完成代码。

关于android - Firebase 数据扁平化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39356529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com