Iterate through nested snapshot children in Firebase using Swift

后端 未结 1 384
梦如初夏
梦如初夏 2021-01-27 03:00

I\'m trying to loop through children in my Firebase Database to retrieve a nested key.

My database is structured like this:

\"Users\" : {
 \"Username\" :         


        
相关标签:
1条回答
  • 2021-01-27 03:59

    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)
            }
        }
    })
    
    0 讨论(0)
提交回复
热议问题