【问题标题】:an alert function that i created is calling several times for no reason我创建的警报功能无缘无故地调用了几次
【发布时间】:2021-09-08 10:51:35
【问题描述】:

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

这是我的注册功能;

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 函数正在调用 signIn 函数,如果一切正常,则 signIn 函数调用 performSegue 函数但在 performSegue 之后

snapshot.hasChild(self.userNameTxtField.text!)

多次触发并发出警报;

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

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

【问题讨论】:

    标签: swift firebase firebase-realtime-database uialertcontroller uialertaction


    【解决方案1】:

    替换

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2022-08-13
    • 1970-01-01
    相关资源
    最近更新 更多