简体   繁体   English

Firebase数据返回无

[英]Firebase Data Returning Nil

I am trying to create programmatic Radio Buttons based on dynamic Firebase data. 我正在尝试基于动态Firebase数据创建程序化Radio Buttons The number of Radio Buttons is dependent on an integer value stored in Firebase, named numberOfChildren . Radio Buttons的数量取决于Firebase中存储的名为numberOfChildren的整数值。

The value I am receiving from Firebase is coming back nil and I cannot figure out why. 我从Firebase收到的值返回nil ,我不知道为什么。 Any help on how to resolve this issue so that I can return an integer value would be appreciated: 任何有关如何解决此问题的帮助,以便我可以返回整数值,将不胜感激:

import UIKit
import FirebaseDatabase
import DLRadioButton


 class PollController: UIViewController {

@IBOutlet weak var passLabel: UILabel!
@IBOutlet weak var pollImage: UIImageView!

var ref: FIRDatabaseReference!
var pollRef: FIRDatabaseReference!

var pass = ""
var passedImageURL = ""

var posX = 0;
var posY = 0;

var numberOfChildren: Int!

let label2 = UILabel(frame: CGRect(x: 90, y: 160, width: 200, height: 70))

override func viewDidLoad() {
    super.viewDidLoad()
    ref = FIRDatabase.database().reference()
    pollRef = ref.child("Polls").child(pass)
    passLabel.text = pass
    pollImage.sd_setImage(with: URL(string: passedImageURL), placeholderImage: UIImage(named: "test"))

    pollRef.observe(FIRDataEventType.value, with: {(snapshot) in
        self.numberOfChildren = Int(snapshot.childSnapshot(forPath: "answers").childrenCount)
        self.passLabel.text = String(self.numberOfChildren)
        print(self.numberOfChildren)
    })

    var buttons = [DLRadioButton]()

    for x in 0..<self.numberOfChildren {
        let firstRadioButton = self.createRadioButton(frame: CGRect(x: CGFloat(x)*32, y: self.view.center.y, width: 40.0, height: 20.0), title: String(x), color: UIColor.green)
        firstRadioButton.tag = x
        buttons.append(firstRadioButton)
        self.view.addSubview(firstRadioButton);
    }

    let groupButtons = DLRadioButton()
    groupButtons.otherButtons = buttons

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

private func createRadioButton(frame : CGRect, title : String, color : UIColor) -> DLRadioButton {
    let radioButton = DLRadioButton(frame: frame);
    radioButton.titleLabel!.font = UIFont.systemFont(ofSize: 14);
    radioButton.setTitle(title, for: UIControlState.normal);
    radioButton.setTitleColor(color, for: UIControlState.normal);
    radioButton.iconColor = color;
    radioButton.indicatorColor = color;
    radioButton.contentVerticalAlignment = UIControlContentVerticalAlignment.center;
    radioButton.addTarget(self, action: #selector(self.logSelectedButton(_:)), for: UIControlEvents.touchUpInside);
    return radioButton;
}

@objc private func logSelectedButton(_ sender: DLRadioButton){

    print("Selected Button Tag = \(sender.tag) and Title \(sender.titleLabel?.text)")
   }

}

The problem is in the way you nest the code. 问题在于您嵌套代码的方式。 Firebase loads the data asynchronously, that's why you pass in a callback block: so that it can call your code block once the data has loaded. Firebase异步加载数据,这就是您传递回调块的原因:这样,一旦加载数据,它便可以调用您的代码块。 By the time you look over self.numChildren that data hasn't loaded yet. 在您查看self.numChildren ,该数据尚未加载。

The solution is to move the code that requires numChildren into the callback block. 该解决方案是移动需要的代码numChildren 回调块。

pollRef.observe(FIRDataEventType.value, with: {(snapshot) in
    self.numberOfChildren = Int(snapshot.childSnapshot(forPath: "answers").childrenCount)
    self.passLabel.text = String(self.numberOfChildren)

    var buttons = [DLRadioButton]()

    for x in 0..<self.numberOfChildren {
        let firstRadioButton = self.createRadioButton(frame: CGRect(x: CGFloat(x)*32, y: self.view.center.y, width: 40.0, height: 20.0), title: String(x), color: UIColor.green)
        firstRadioButton.tag = x
        buttons.append(firstRadioButton)
        self.view.addSubview(firstRadioButton);
    }

    let groupButtons = DLRadioButton()
    groupButtons.otherButtons = buttons
})

This way the loop is only invoked once numChildren has been initialized from the database. 这样,仅在从数据库初始化了numChildren才调用循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM