ios - 创建 firebase 数据结构时遇到问题

标签 ios swift firebase firebase-authentication nosql

对于我的 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/

相关文章:

ios - 当我运行我的应用程序时,我收到此错误。线程 1 : Fatal error: Unexpectedly found nil while unwrapping an Optional value

iphone - 开发者为什么要关心 ios 设备是否越狱

swift - 将类型从类型数组传递到泛型方法

android - 无法在不同的 PC 上使用 Firebase 登录 - 错误的 OAuth2 相关配置

ios - React-Native:Firebase 错误:未创建 Firebase 应用程序 [默认] - 调用 Firebase App.initializeApp() (app/no-app) site:stackoverflow.com

android - 如何通过 Wireshark 调试 Firebase 问题?

ios - 使用 NSPredicate 比较两个数组

ios - xcode项目中可执行文件的扩展名是什么

ios - Swift View Controller 初始化程序问题

macos - 如何在同一个 Mac 窗口中显示一个新的 View Controller ?