对于我的 Firebase 数据结构,我希望它能够处理如下内容:
用户可以创建一种记录想法的方式。他们可以随意调用它。 Mary Chen
传递参数 journal
,而 Mr Owner
想要将其命名(传递参数)log
。 Firebase 将它们保存为自己的树,将其命名为用户要求命名的任何参数。
然后出现另一棵树:whenLoggedIn +\(parameter)
。该树将 yyyy-mm-dd
日期保存到相应的帖子中。
例子:
{
"uids": {
"D0Ez8edYhIbTuUfmIeO0KOq5xVB3": {
"friends": {
"IbTuUfmIeO0KOBr5Q4gAqD": true
},
"name": "Mary Chen",
"journal": {
"entry 1- trader joes": "went to store! :)",
"entry 2- ate sushi": "took out the garbage today then got free sushi from trader joes!!!"
},
"whenLoggedInJournal": {
"1": "1997-12-25",
"2": "2016-2-23"
}
},
"L8kBHaGBr5Q4gAqDOhFY29Okepm1": {
"friends": {
"D0Ez8edYhIbTuUfmIeO0KOq5xVB3": true
},
"name": "Mr Owner",
"journal": {
"log 1": "spotted some drunkard in my store",
"log 2": "drainage pipe clogged with tomatos, I suspect the drunkard",
"log 3": "did inventory check, 1 less sushi box, suspect the drunkard"
},
"whenLoggedInLog": {
"1": "1997-12-25",
"2": "2016-2-27",
"3": "2016-4-2"
}
}
}
}
我通读了 Firebase 指南上的“构建数据”,但我不明白如何一次添加树。我还想实现一个扁平化的数据集;这对于我正在做的事情是必要的吗?
最佳答案
推荐的 Firebase 数据结构是将每个实体拉到它自己的顶级节点。所以在你的情况下,这会导致:
{
"userNames": {
"D0Ez8edYhIbTuUfmIeO0KOq5xVB3": "Mary Chen",
"L8kBHaGBr5Q4gAqDOhFY29Okepm1": "Mr Owner"
},
"userFriends": {
"D0Ez8edYhIbTuUfmIeO0KOq5xVB3": {
"IbTuUfmIeO0KOBr5Q4gAqD": true
},
"L8kBHaGBr5Q4gAqDOhFY29Okepm1": {
"D0Ez8edYhIbTuUfmIeO0KOq5xVB3": true
}
},
"userJournals": {
"D0Ez8edYhIbTuUfmIeO0KOq5xVB3": {
"entry 1- trader joes": "went to store! :)",
"entry 2- ate sushi": "took out the garbage today then got free sushi from trader joes!!!"
},
"L8kBHaGBr5Q4gAqDOhFY29Okepm1": {
"log 1": "spotted some drunkard in my store",
"log 2": "drainage pipe clogged with tomatos, I suspect the drunkard",
"log 3": "did inventory check, 1 less sushi box, suspect the drunkard"
}
},
"whenLoggedInJournals": {
"D0Ez8edYhIbTuUfmIeO0KOq5xVB3": {
"1": "1997-12-25",
"2": "2016-2-23"
},
"L8kBHaGBr5Q4gAqDOhFY29Okepm1": {
"1": "1997-12-25",
"2": "2016-2-27",
"3": "2016-4-2"
}
}
}
这个结构:
- 使加载部分用户数据变得更容易,例如只是名字
- 可以更轻松地保护数据,例如公开用户名,他们自己和他们的 friend 可以查看他们的 friend 列表,日志和登录数据只能由他们自己查看
关于ios - 创建 firebase 数据结构时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38034952/