1

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!!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
LSteele
  • 53
  • 1
  • 6
  • I think you have a wrong Firebase database structure. In firebase, databases are key-valued. See example here: http://stackoverflow.com/a/16423316/743923 – Dani Pralea Mar 19 '17 at 21:15
  • You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Mar 19 '17 at 23:12
  • @FrankvanPuffelen, thank you for showing me how to do that! I've added the JSON text as you suggested. – LSteele Mar 20 '17 at 03:12
  • @DanutPralea, thank you for the insight - it's made me think about this in a different way. I'm going to look into the link you gave me, and re-read in the Firebase Docs about structure. – LSteele Mar 20 '17 at 03:19
  • awesome! I'm glad I helped :) I know it's different than to a standard database structure, but once you get used to it, it's really easy to move forward – Dani Pralea Mar 20 '17 at 12:48

1 Answers1

2

The problem in your code is that snapshot refers to the Favorites node – instead of looking for LocationName there, you should look for it inside each of the Location child nodes. Therefore your loop should look something like this:

let databaseHandle = databaseRef.observe(.value, with: { snapshot in
    for child in snapshot.children {
        let childSnapshot = snapshot.childSnapshotForPath(child.key)
        if let dbLocation = childSnapshot.value["LocationName"] as? String {
            print(dbLocation)
        }
    }
})
Incinerator
  • 2,589
  • 3
  • 23
  • 30