更新存储在Firebase - Swift中随机生成的键中的值

时间:2017-07-27 23:36:52

标签: swift uitableview firebase firebase-realtime-database

目前我正在尝试更新许多警报值,例如标签,时间,重复日期等。因此,在我的应用程序中,一旦用户创建警报,它将显示在tableview中。一旦他们在tableview上单击此警报,他们就应该能够编辑这些值。所有这些都在UI部分中工作,但是在firebase中保存我对此警报所做的编辑时,会创建一个新的警报对象。我想在编辑(并保存)后更新此警报的值,而不是添加新的警报。

以下是我的应用的Firebase中的结构:

enter image description here

以下是我的代码片段,向您展示我尝试过的操作:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "save" {

            var alarmRefKey = alarmRef.key //the unique alarm key

            let displayAlarms = segue.destination as! DisplayAlarms //unwind segue destination

            if alarm != nil {//alarm is the object, so if there is an alarm, you can edit it
                print("There is already an alarm.")
                let timeFormatter = DateFormatter()
                timeFormatter.timeStyle = DateFormatter.Style.short

                timePicker.addTarget(self, action: Selector(("handler:")), for: UIControlEvents.valueChanged)

                let strDate: String? = timeFormatter.string(from: timePicker.date)
                print("\(strDate!) is the time!")
                alarm?.time = strDate!
                alarm?.alarmLabel = updateLabelText.text ?? ""

                self.tableView.reloadData()

                let key = alarmRefKey

                print("\(key) is the alarm key")
                var ref: DatabaseReference
                ref = Database.database().reference()
                let currentUserID = Auth.auth().currentUser?.uid//current user's id


//                let parameters: Any? = ["alarmLabel": updateLabelText.text!, "alarmTime": strDate!, "userID": currentUserID!, "repeatedDays": weekdaysSelected]

                //where I am having trouble.
                //updating values in the alarm associated with this key.
                ref.child("alarms").child(key).updateChildValues(["alarmLabel": updateLabelText.text!, "alarmTime": strDate!, "userID": currentUserID!, "repeatedDays": weekdaysSelected])

            }

0 个答案:

没有答案