Firebase将数据插入数据库三次而不是一次

时间:2018-01-17 16:34:50

标签: ios swift firebase firebase-realtime-database

由于某些原因,当我通过我的应用程序中的表单向Firebase数据库添加数据时,它会将数据保存到数据库中,但不是一次,而是只保留一次。

我无法弄明白为什么,因为我之前使用过此代码并且工作正常...

代码:

@IBAction func createPostTapped(_ sender: UIButton) {
   if let uid = Auth.auth().currentUser?.uid {
        Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: {
            (snapshot) in
              if let userDictionary = snapshot.value as? [String: AnyObject] {
                  for user in userDictionary {
                      if let username = user.value as? String {
                          if let game = self.gameTextField.text {
                              if let activity = self.activityTextField.text {
                                if let console = self.consoleTextField.text {
                                    if let skill = self.skillTextField.text {
                                        if let communication = self.communicationTextField.text {
                                            if let lfglfm = self.lfglfmTextField.text {
                                                if let description = self.descriptionTextView.text {

                                                    let postObject: Dictionary<String, Any> = [
                                                        "uid" : uid,
                                                        "username" : username,
                                                        "game" : game,
                                                        "activity" : activity,
                                                        "console" : console,
                                                        "skill" : skill,
                                                        "communication" : communication,
                                                        "lfglfm" : lfglfm,
                                                        "description" : description
                                                        ]

                                                    Database.database().reference().child("posts").childByAutoId().setValue(postObject)

                                                    let alert = UIAlertController(title: "Success!", message: "Your post was added successfully.", preferredStyle: .alert)
                                                    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                                                        //code will run when ok button is pressed
                                                        let vc = self.storyboard?.instantiateViewController(withIdentifier: "LoggedInVC")
                                                        self.present(vc!, animated: true, completion: nil)
                                                    }))
                                                    self.present(alert, animated: true, completion: nil)

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        })
    }
}

如果有人知道为什么我的代码会将数据发布三次而不是一次,我会很感激帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

我猜是这行代码:

for user in userDictionary {

看起来你在该节点内有3个条目,所以

Database.database().reference().child("posts").childByAutoId().setValue(postObject)

实际执行3次。