Firebase/Swift: childSnapshotForPath 访问autoID 数据?

Firebase/Swift: childSnapshotForPath access autoID data?

我目前正在努力访问存储在路径中的其他数据,更具体地说是子文件夹中的数据。我的设置如下所示:

userID{
    username:Andreas,
    gender:Male,
    age:18,
    footballTeams{
       Team1{
           name:My team 1
           matchesPlayed:3
       }
    }
}

所以我目前运行使用此代码获取用户名、性别和年龄:

ref.child("Users").observeEventType(.ChildAdded, withBlock: { (snapshot1:FIRDataSnapshot) in                   
      //code goes here..
      var username = String(snapshot1.value!["username"] as! String)
      var gender = String(snapshot1.value!["gender"] as! String)
      var age = Int(snapshot1.value!["age"] as! Int)                  
})

但是,我想获取存储在 footballTeams 路径中的所有球队名称,顺序如 'Team1, Team2, Team3' 等。所以我偶然发现了“snapshot1.childSnapshotForPath("footballTeams/Team1").value!["name"]”。但是,只要我知道每个团队名称路径的确切名称,这就可以工作,但这是作为自动 ID 存储的。关于我将如何处理这个问题有什么想法吗?

提前致谢。

选择与 childSnapshotForPath 合作时,您的选择是正确的。您只需获取 /footballTeams 分支快照并对其进行迭代以获取每个子节点。

ref.child("Users").observeEventType(.ChildAdded, withBlock: { (snapshot1:FIRDataSnapshot) in                   
      var username = String(snapshot1.value!["username"] as! String)
      var gender = String(snapshot1.value!["gender"] as! String)
      var age = Int(snapshot1.value!["age"] as! Int)
      if let footballTeamsSnapshot = snapshot1.childSnapshotForPath("footballTeams") as? FIRDataSnapshot {
        for child in footballTeamsSnapshot.children.allObjects as [FDataSnapshot] {
            print(child.value)
        }
      }         
})