简体   繁体   English

如何在Firebase Realtime数据库中嵌套数据(Swift)

[英]How to nest data in Firebase Realtime database (Swift)

My database in Firebase currently looks like this: 我在Firebase中的数据库当前如下所示:

locations: {
    person: ""
    location: ""
}

How do I nest the data so that it looks something like this: 如何嵌套数据,使其看起来像这样:

locations: {
    person: "" {
        location: ""
}
}

A structure where locations is the parent, person is the child, and location is the child of person. 一个结构,其中location是父级,person是子级,而location是person的子级。

I know the relationships, I just am not too sure of the syntax. 我知道这些关系,只是我不太确定语法。

Here is the code I currently have, written in Swift: 这是我目前用Swift编写的代码:

let locRef = locationRef.childByAutoId()
let locItem = [
    "location": getLocation(),
    "person": senderId
]

locRef.setValue(locItem)

Thanks. 谢谢。

let locRef = locationRef.childByAutoId()
let locItem = [
    senderId : [
        "location": getLocationID()
    ]
]

SenderID will be the key for the person, so you can find the Person by its ID... And the ID is holding a nested location key SenderID将是该人员的密钥,因此您可以通过其ID查找该人员...并且该ID包含嵌套的位置密钥

Do you only need the logic? 您只需要逻辑吗? Or do you need working code example? 还是需要工作代码示例?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM