Ahad Sheriff Ahad Sheriff - 4 years ago 105
Swift Question

How to nest data in Firebase Realtime database (Swift)

My database in Firebase currently looks like this:

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.

I know the relationships, I just am not too sure of the syntax.

Here is the code I currently have, written in Swift:

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

locRef.setValue(locItem)


Thanks.

Neo Neo
Answer Source
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

Do you only need the logic? Or do you need working code example?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download