[英]Firebase getting wrong snapshot back when asking for child with more children?
I am having trouble grabbing my data from Firebase. 我无法从Firebase获取数据。 I have setup three test data sets, each item is called a "Campaign".
我设置了三个测试数据集,每个项目称为“广告系列”。 For every "Campaign" there can be a user that has completed the campaign which will follow under the campaign id -> users -> userid -> confirmImageUrl and the image that has been completed.
对于每个“广告系列”,可以有一个已完成广告系列的用户,该用户将遵循广告系列ID->用户->用户ID-> ConfirmImageUrl和已完成的图像。 When I try to pull the users child and the child of the users, I am not getting all the data.
当我尝试拉用户子和用户的子时,我没有得到所有数据。 When there are no users, it is pulling it correclty.
如果没有用户,它将使其正确。
In Firebase this is how it is setup: Firebase Data Setup 在Firebase中,它是这样设置的: Firebase Data Setup
In my viewDidLoad
, this is how I am pulling the data. 在我的
viewDidLoad
,这就是我提取数据的方式。 Each "campaign" goes into a UITableViewCell
: 每个“广告系列”都进入
UITableViewCell
:
DataService.ds.DATABASE_BASE_CAMPAIGNS.observe(.value, with: { (snapshot) in
print("menan check:\(snapshot.value!)")
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
print("menan check: \(snapshot)")
self.campaigns = []
for snap in snapshot {
if let campaignData = snap.value as? Dictionary <String, AnyObject> {
let key = snap.key
let campaign = Campaigns(campaignKey: key, campaignData: campaignData)
print("menan check: \(campaignData)")
self.campaigns.append(campaign)
}
}
}
self.tableView.reloadData()
})
This is the output: The Output from the snapshot received 这是输出: 收到的快照的输出
I have spent quite a long time trying to figure it out? 我花了很长时间试图弄清楚吗? Anyone know whats wrong?
有人知道怎么了吗?
Your data is pretty deep so you will need a couple more Dictionaries to work with the key value pairs. 您的数据非常深,因此您将需要更多词典来使用键值对。
Here's a quick solution to get to the deepest piece of data, the confirm value. 这是获取最深层数据(确认值)的快速解决方案。 It could be shortened but I left it verbose so it could be explained line by line.
可以将其缩短,但我将其保留为冗长,因此可以逐行解释。
campaignRef.observe(.value, with: { (snapshot) in
for snap in snapshot.children { //iterate over 21hb, 989 huv etc
//contains all data in 21hb
let node = snap as! FIRDataSnapshot
// represent the data in the node as a dictionary
let nodeDict = node.value as! [String: AnyObject]
//grab just the users node, whose value is also a dictionary
let users = nodeDict["users"] as! [String: AnyObject]
//iterate over each user in the users node
for user in users {
let userId = user.key //this is the user id
//the value is a dictionary of key: value
// as in confirm: url
let value = user.value as! [String: AnyObject]
//get the value for the confirm key, which is the url
let confirm = value["confirm"]
//print the user it and the confirm url
print("key = \(userId) value = \(confirm!)")
}
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.