I am new on Swift and Firebase, but I am building an app that uses in one view controller a picker view and the data to populate it comes from an Firebase structure that put data into an array with parameters stored in a subclass.
I can see all the data printed in the debugger console, (Firebase is OK running here) showing me that the array received data from Firebase (and I can insert new items using a method for this with a button in this new controller too), but it looks that the methods for pickerview (numberOfCOmponents
, numberOfRowsInComponent
and titleforRow
) initialize before the viewDidLoad
, where the method who brings the data from Firebase to the array occur, and I have a count = 0
.
My pickerview stays empty. I have no crash. At the debugger console, I can see the printed count = 0
and then, with a print
placed at the end of viewDidLoad
method, it counts how many registers I actually have and show them.
I want to know what should I do for the array be fulfilled with Firebase data before pickeview methods do it, and these data como to the pickerview rows. Thank you.
var refw2: [PickerArrayClasses] = []
@IBOutlet weak var pckProfessores: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
let refW = FIRDatabase.database().reference().child("avaliacao").child("equipe")
refW.queryOrdered(byChild: "nomePreceptor").observe(.value, with: {(snapshot) in
var newTeste2: [PickerArrayClasses] = []
for resposta in snapshot.children {
let itemsAadicionar = PickerArrayClasses(snapshot: resposta as! FIRDataSnapshot)
newTeste2.append(itemsAadicionar)
}
self.refw2 = newTeste2
print("ITENS NO ARRAY RefW2 \(newTeste2)")
})
self.pckProfessores.delegate = self
self.pckProfessores.dataSource = self
self.pckAvaliador.delegate = self
self.pckAvaliador.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
print("CONTAR ITENS NO REFW2 - SECAO COUNT = \(refw2.count)")
return refw2.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return refw2[row].nomePrecep
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let PreceptorEscolhido = refw2[row].nomePrecep
print("PRECEPTOR ESCOLHIDO: \(PreceptorEscolhido)")
}
Information at the Debugger Console
CONTAR ITENS NO REFW2 - SECAO COUNT = 0
ITENS NO ARRAY RefW2
[ChkList.PickerArrayClasses(nomePrecep: Optional("2222222222"), key: -KYiOkUH1cFXj7q7LkTi),
ChkList.PickerArrayClasses(nomePrecep: Optional("33333"), key: -KYiOvOjfxZpuUkxM4bn),
ChkList.PickerArrayClasses(nomePrecep: Optional("5555555555555555555"), key: -KYiPsHTumA0qdTKkbMT),
ChkList.PickerArrayClasses(nomePrecep: Optional("77777"), key: -KYikQ5dveM3L7JielTt),
ChkList.PickerArrayClasses(nomePrecep: Optional("MARK BONINHA"), key: -KYi7XofWn9Um6FVV3M6),
ChkList.PickerArrayClasses(nomePrecep: Optional("OUTRO MARK"), key: -KYi7t6OEllWTmMl655T),
ChkList.PickerArrayClasses(nomePrecep: Optional("dfddf"), key: -KYiI8sTLE6G3olzOtAJ)]
I could solve this issue adding the following line into viewDidLoad method:
self.pckProfessores.reloadAllComponents()
just after self.refw2 = newTeste2
line.