无法创建firebase数据结构

时间:2016-06-26 03:27:30

标签: ios swift firebase firebase-authentication nosql

对于我的Firebase数据结构,我希望它能够处理这样的事情:

用户可以创建记录思路的方法。他们可以随意调用它。 Mary Chen传递参数journalMr 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指南中阅读了“构建数据”,但我没有掌握如何一次添加树。我还想实现扁平的数据集;那对我正在做的事情是必要的吗?

1 个答案:

答案 0 :(得分:1)

推荐的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"
        }
    }
}

这个结构:

  • 可以更轻松地加载部分用户数据,例如只是名字
  • 可以更轻松地保护数据,例如使用户名称公开,他们的朋友列表可以自己查看,他们的朋友和日记和登录数据只能自己查看