我正在尝试从firebase中提取数据并使用下面显示的方法放入数组中。当像我建议的那样使用错误完成处理程序时,我将Data的值分配给数组locData。 get locdata函数一直在底部。
import UIKit
import Firebase
class SearchSelectVC: UIViewController, UIPickerViewDelegate , UIPickerViewDataSource{
@IBOutlet weak var locationPicker: UIPickerView!
@IBOutlet weak var humidityPicker: UIPickerView!
@IBOutlet weak var datePicker: UIPickerView!
@IBOutlet weak var locationLB: UILabel!
@IBOutlet weak var humidityLB: UILabel!
@IBOutlet weak var dateLB: UILabel!
var locData: [String] = [String]()
var humidityData : [String] = [String]()
var dateData : [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// initPickers()
humidityPicker.delegate = self
humidityPicker.dataSource = self
locationPicker.delegate = self
locationPicker.dataSource = self
datePicker.delegate = self
datePicker.dataSource = self
//locData = [ "hi" , "hi2 ","hi3","hi4", "hi5", "hi6", "hi7"]
//dateData = [ "no" , "no2 ","no3","no4", "no5", "no6", "no7"]
//humidityData = [ "yes" , "yes2 ","yes3","yes4", "yes5", "yes6", "yes7"]
//datePicker.isHidden = true
//locationPicker.isHidden = true
//humidityPicker.isHidden = true
getLoc(onCompletion: { (data) in
print(data)
self.locData = data
}, onError: { (error) in
})
print(locData)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
if(pickerView == locationPicker){
return 1
}
else if(pickerView == datePicker){
return 1
}
else if(pickerView == humidityPicker){
return 1
}
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView == locationPicker) {
return locData.count
}
else if (pickerView == humidityPicker){
return humidityData.count
}
else if (pickerView == datePicker) {
return dateData.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView == locationPicker) {
return locData[row]
}
else if(pickerView == datePicker){
return dateData[row]
}
if(pickerView == humidityPicker){
return humidityData[row]
}
return "thisisbad"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if(pickerView == locationPicker){
updateLabelLoc()
getDate(onCompletion: { (data) in
print(data)
self.dateData = data
}, onError: { (error) in
})
}
else if(pickerView == datePicker){
updateLabelDate()
getHumidity(onCompletion: { (data) in
print(data)
self.humidityData = data
}, onError: { (error) in
})
}
else if(pickerView == humidityPicker){
updateLabelHumidity()
}
}
func updateLabelLoc(){
let location = locData[locationPicker.selectedRow(inComponent: 0)]
locationLB.text = location
}
func updateLabelDate(){
let date = dateData[datePicker.selectedRow(inComponent: 0)]
dateLB.text = date
}
func updateLabelHumidity(){
let humidity = humidityData[datePicker.selectedRow(inComponent: 0)]
humidityLB.text = humidity
}
func getDate(onCompletion: @escaping (Array<String>) -> Void, onError: @escaping (NSError) -> Void) {
var data : [String] = []
let selectedLoc = locationLB.text
var dateRef: DatabaseReference!
dateRef = Database.database().reference().child("launch").child((selectedLoc)!)
dateRef.observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
data.append(key)
print("date = \(key)")
print(data)
onCompletion(data)
}
})
}
func getLoc(onCompletion: @escaping (Array<String>) -> Void, onError: @escaping (NSError) -> Void){
var data : [String] = []
var locRef: DatabaseReference!
locRef = Database.database().reference().child("launch")
locRef.observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
data.append(key)
print("location = \(key)")
print(data)
onCompletion(data)
}
})
}
func getHumidity(onCompletion: @escaping (Array<String>) -> Void, onError: @escaping (NSError) -> Void){
var data : [String] = []
let selectedLoc = locationLB.text
let selectedDate = dateLB.text
var humidityRef: DatabaseReference!
humidityRef = Database.database().reference().child("launch").child((selectedLoc)!).child((selectedDate)!)
humidityRef.observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
data.append(key)
print("humidity = \(key)")
print(data)
onCompletion(data)
}
})
}
func initPicker(onCompletion: @escaping (Array<String>) -> Void, onError: @escaping (NSError) -> Void){
humidityPicker.delegate = self
humidityPicker.dataSource = self
locationPicker.delegate = self
locationPicker.dataSource = self
datePicker.delegate = self
datePicker.dataSource = self
}
}
我不确定我是否正确分配它但是当我尝试打印locData时,没有任何反应。此外,当我运行程序时,我的UIPickerViews没有出现。
答案 0 :(得分:0)
observeSingleEvent
是异步调用,因此您需要在函数completionHandler
上创建getDate
,该方法在方法完成时将被调用:
func getDate(onCompletion: @escaping ([String]) -> Void) {
var data : [String] = []
let selectedLoc = locationLB.text
var dateRef: DatabaseReference!
dateRef = Database.database().reference().child("launch").child((selectedLoc)!)
dateRef.observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
data.append(key)
print("date = \(key)")
print(data)
onCompletion(data)
}
})
}
要打电话:
getDate(onCompletion: { (data) in
print(data)
}