I'm trying to loop through children in my Firebase Database to retrieve a nested key.
My database is structured like this:
"Users" : {
"Username" : {
"Favorites" : {
"Location" : {
"Latitude" : 123,
"LocationName" : "San Francisco",
"Longitude" : 123
},
"Location2" : {
"Latitude" : 123,
"LocationName" : "London",
"Longitude" : 123
}
}
}
}
I am trying to print out all of the "LocationName" keys, and am able to print one instance of this key, but not able to loop through and print all instances of this key.
I'm not sure where in my for loop I'm going wrong?
The code I'm working with is below.
FIRApp.configure()
let databaseRef = FIRDatabase.database().reference().child("Users").child("Username").child("Favorites")
let databaseHandle = databaseRef.observe(.value, with: { (snapshot) in
for item in snapshot.children {
if let dbLocation = snapshot.childSnapshot(forPath: "LocationName") as? String {
print (dbLocation)
}
print(item)
}
})
I'm very new to Swift, and even newer to Firebase, so any help would be greatly appreciated!!