我创建的警报功能无缘无故地调用了几次

an alert function that i created is calling several times for no reason

我正在使用 firebase 数据库开发一个项目,我用他们的 displayName 而不是 uid 来保存用户。我检查尝试注册的用户的显示名称是否在 firebase 的显示名称列表中,如果不在,我允许用户使用该显示名称注册。

这是我的注册功能;

func signUp(email: String, password: String) {
    //gets the user data in the firebase
    Database.database().reference().child("users").observe(.value) { [self] (snapshot) in
        if snapshot.hasChild(self.userNameTxtField.text!){ //checking the displayName
            self.present(alertFunction(message: "Seçmiş olduğunuz kullanıcı adı daha önceden alınmış."), animated: true)
        } else {
            Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
                if let error = error as NSError? {...} else {
                    self.signIn(email: email, password: password)
                }
            }
        }
    }
}

这是我的登录功能;

    func signIn(email: String, password: String) {
    
    Auth.auth().signIn(withEmail: email, password: password) { (authResult, error) in
        if let error = error as NSError? {...} else {
        
        self.activityIndicator.stopAnimating()
        self.view.isUserInteractionEnabled = true
        //create database reference
        self.ref = Database.database().reference()
        //create new child and write a value on the existance child
        if self.userNameTxtField.text == ""{
            print("User Name can not be nill!")
            self.present(self.alertFunction(message: "Kullanıcı adı boş bırakılamaz"), animated: true)
        } else {
            UserDefaults.standard.setValue(true, forKey: "userSignedIn")
            if let currentUser = Auth.auth().currentUser?.createProfileChangeRequest() {
                currentUser.displayName = self.userNameTxtField.text!
                currentUser.commitChanges(completion: { error in
                    if let error = error {
                        print(error)
                    } else {
                        print("DisplayName changed")
                        self.ref.child("users").child(currentUser.displayName!).setValue(["nickname": self.userNameTxtField.text ?? ""])
                        self.ref.child("users/\(currentUser.displayName!)/step_count").setValue(Int(0))
                        self.ref.child("users/\(currentUser.displayName!)/total_point").setValue(Int(0))
                        self.ref.child("users/\(currentUser.displayName!)/onesignal_player_id").setValue("")

                        self.performSegue(withIdentifier: "showMainFromRegister", sender: self)
                        }
                    })
                }
            }
        }
    }
}

最后是我的 actionFunction;

func alertFunction(message: String) -> UIAlertController {
    let alert = UIAlertController(title: "Uyarı!", message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Tamam", style: .cancel, handler: nil))
    self.activityIndicator.stopAnimating()
    self.view.isUserInteractionEnabled = true
    
    return alert
}

我在单击按钮后调用 signUp 函数并且 signUp func 正在调用 signIn 函数,如果一切正常,signIn 函数调用 performSegue 函数但在 performSegue 之后

snapshot.hasChild(self.userNameTxtField.text!)

被多次触发并发出警报;

self.present(alertFunction(message: "Seçmiş olduğunuz kullanıcı adı daha önceden alınmış."), animated: true)

无故显示。 我该如何解决这个问题?

替换

// listens for any change
.observe(.value) { [self] (snapshot) in

// listens for a change only once 
.observeSingleEvent(of: .value, with: { [weak self] snapshot in