在要求有更多孩子的孩子时,Firebase会收回错误的快照?

时间:2017-01-10 15:13:46

标签: ios swift firebase firebase-realtime-database

我无法从Firebase获取数据。我已经设置了三个测试数据集,每个项目被称为" Campaign"。对于每一个" Campaign"可以有一个用户完成了广告系列,该广告系列将在广告系列ID下跟随 - >用户 - > userid - > confirmImageUrl和已完成的图像。当我试图拉动用户的孩子和用户的孩子时,我没有获得所有数据。当没有用户时,它正在拉扯它。

在Firebase中,这是它的设置方式: Firebase Data Setup

在我的viewDidLoad中,这就是我提取数据的方式。每个"广告系列"进入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()
})

这是输出: The Output from the snapshot received

我花了很长时间试图解决这个问题?任何人都知道什么是错的?

1 个答案:

答案 0 :(得分:0)

您的数据非常深,因此您需要使用更多字典来处理键值对。

这是一个快速解决方案,可以获得最深的数据,即确认值。它可以缩短,但我把它留下了冗长,所以可以逐行解释。

    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!)")
                }
            }
    })