第一个IBAction触发器将nil写入数组。第二次触发从第一次引用写入

时间:2018-02-14 10:15:49

标签: arrays swift google-cloud-firestore

单击IBAction(viewScheduleButtonTapped)以从Firestore db读取数组时,返回'nil'。我必须再次单击它才能将数据写入数组。但是,写入的数据来自第一个(IBAction click)Firestore文档引用。 (pickerview选择Firestore文档参考)。

从第一次触发打印

  

scheduleIDOutput是test2 []

从第二次触发打印

  

scheduleIDOutput是test3 [“[[\”13 \“,\”13 \“,\”13 \“,\”13 \“,\”0 \“,\”0 \“,\”0 \ “]]”]“

但这是来自文档“test2”的数据

这是代码(删除了tableview代码,因为它还没有做任何事情)

import UIKit
import Firebase
import FirebaseFirestore

class ViewScheduleViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var myTextfield: UITextField!


    var pickerData: [String] = [String]()

    var startHourArray: [String] = []
    var stopHourArray: [String] = []

    var startHourBreakArray: [String] = []
    var stopHourBreakArray: [String] = []

    var weekdayArray = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

    var scheduleIDOutput: String? = ""
    var scheduleStartDateOutput: Double? = nil


    //PickerView setup
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerData[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        myTextfield.text = pickerData[row]
        scheduleIDOutput = myTextfield.text
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        let myPicker = UIPickerView()
        myTextfield.inputView = myPicker


        // Ask db for a list of documents in collection
        let db = Firestore.firestore()
        db.collection("schedules").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {

                    self.pickerData.append(document.documentID)
                }
            }

            print(self.pickerData)

        }

        myPicker.delegate = self
        myPicker.dataSource = self

    }

    @IBAction func viewScheduleButtonTapped(_ sender: Any) {

       // Fetch selected document from db
        let db = Firestore.firestore()
        let docRef = db.collection("schedules").document("\(scheduleIDOutput ?? "")")

        docRef.getDocument { (docSnapshot, error) in

            guard let docSnapshot = docSnapshot, docSnapshot.exists else { return }
            let data = docSnapshot.data()

            let startHourInput = data["starttime"] as? String ?? ""
            let stopHourInput = data["stoptime"] as? String ?? ""

            let startHourBreakInput = data["startbreak"] as? String ?? ""
            let stopHourBreakInput = data["stopbreak"] as? String ?? ""

            let scheduleStartDateInput = data["schedulestartdate"] as? Int

            let viewSchedule = Schedule(scheduleStartDate: scheduleStartDateInput!, scheduleStartTime: startHourInput, scheduleStopTime: stopHourInput, scheduleStartBreak: startHourBreakInput, scheduleStopBreak: stopHourBreakInput)

            self.startHourArray = [viewSchedule.scheduleStartTime]
            self.stopHourArray = [viewSchedule.scheduleStopTime]

            self.startHourBreakArray = [viewSchedule.scheduleStartBreak]
            self.stopHourBreakArray = [viewSchedule.scheduleStopBreak]


        }


     print("scheduleIDOutput is \(scheduleIDOutput  ?? "empty") \(startHourArray)")

}
}

0 个答案:

没有答案