Firebase / Swift:childSnapshotForPath访问自动ID数据? [英] Firebase/Swift: childSnapshotForPath access autoID data?
问题描述
我目前正在努力访问存储在路径中的其他数据,更具体的来自子文件夹的数据。我的设置如下所示:
userID {
用户名:Andreas,
性别:男,
年龄:18,
footballTeams {
Team1 {
name:My team 1
matchesPlayed:3
}
}
} $所以我现在运行这个代码来抓住用户名,性别和年龄:
$ $ $ $ $ $ $ $ $ $ $ $ $ ref $($用户$)$ code $ ..
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 / Te AM1 )。值![ 名称 ]
。不过,只要我知道每个团队的名称路径的确切名称,这将工作,但这是存储为自动ID。任何想法如何我会这样做?
感谢提前。
当您选择使用 childSnapshotForPath
时,您是正确的。你只需要得到 / footballTeams
分支快照并迭代它就可以得到每个孩子。
在
var username = String(snapshot1.value![username] as!)中的observeEventType(.ChildAdded,withBlock:{(snapshot1:FIRDataSnapshot)) )
var gender = String(snapshot1.value![gender] as!String)
var age = Int(snapshot1.value![age] as!Int)
if让footballTeamsSnapshot = snapshot1.childSnapshotForPath(footballTeams)为?FIRDataSnapshot {
for footballTeamsSnapshot.children.allObjects as [FDataSnapshot] {
print(child.value)
}
}
})
I am currently struggling with accessing other data stored in a path, more specific the data from a sub-folder. My setup looks like this:
userID{
username:Andreas,
gender:Male,
age:18,
footballTeams{
Team1{
name:My team 1
matchesPlayed:3
}
}
}
So I am currently running this code to grab both username, gender and age:
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)
})
However, I want to grab all the team names stored in the footballTeams path, ordered like 'Team1, Team2, Team3' etc. So I stumbled upon "snapshot1.childSnapshotForPath("footballTeams/Team1").value!["name"]
". However, this would have worked, as long as I knew the exact name of each team name-path, but this is stored as auto ID. Any ideas on how I would approach this?
Thanks in advance.
You were on the right way when chose to work with childSnapshotForPath
. You just have to get the /footballTeams
branch snapshot and iterate over it to get each child.
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)
}
}
})
这篇关于Firebase / Swift:childSnapshotForPath访问自动ID数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!