2017-10-06 71 views
-1

我試圖從firebase中提取數據並將其放入數組中,使用下面顯示的方法。當使用像我這樣的錯誤完成處理程序時,我將Data的值賦給數組locData。獲取locdata函數完全位於底部。函數沒有返回,Swift Firebase

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

你應該使用一個完成處理程序而不是返回一個數組。即使在添加任何數據之前,您也正在返回數據。 – koropok

回答

0

observeSingleEvent是一個異步調用,所以你需要讓你的函數getDate一個completionHandler當方法執行完畢後,將被稱爲:

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) 
} 
+0

我該如何添加錯誤?對不起,我是新來的。 –

+0

@RohanRao,閱讀關於那個電話我不認爲你需要錯誤部分,所以我刪除它。另外''return'String]'而不是''completionHandler''所以接受和回答並不是真的好,然後刪除接受並更新問題。你應該問一個新的問題。 –

+0

@RohanRao,你可能有1個問題/天 –