@IBAction func signup(_ sender: Any) {
print("began signup process")
guard let fullname = fullnameField.text, fullname != "" else {
print("FULLNAME field is empty")
return
}
guard let username = usernameField.text, username != "" else {
print("USERNAME field is empty")
return
}
guard let email = emailField.text, email != "" else {
print("EMAIL field is empty")
return
}
guard let password = passwordField.text, password != "" else {
print("PASSWORD field is empty")
return
}
print("all fields good")
mainActivityIndicator.startAnimating()
self.checkUsernameAvailability(username: username, completion: {
result in
print("starting check")
...
})
print("finished the function")
}
问题基本上是checkUsernameAvailability函数内部没有任何内容会调用。 控制台看起来像这样:
began signup process
all fields good
finished the function
它不会打印'开始检查'或在函数内部运行任何代码。 这可能是一个菜鸟错误,如果这是一个愚蠢的问题,我很抱歉。 未来的感谢。
P.S我检查了整个控制台,没有任何与此相关的错误。
编辑:这是函数内的代码
func checkUsernameAvailability(username: String, completion: @escaping (Bool) -> Void) {
_ = Database.database().reference().child("usernames").observe(.childAdded, with: {
snapshot in
print("checking username availability")
print(snapshot)
completion(true)
let dict = snapshot.value as? [String: AnyObject]
for handled in (dict?.values)! {
print("stumbled")
print(handled)
if username.lowercased() == handled.lowercased {
completion(false)
}
}
})
}
和...
self.checkUsernameAvailability(username: username, completion: {
result in
print("starting check")
if result == false {
print("username is taken")
self.mainActivityIndicator.stopAnimating()
return
} else {
Auth.auth().createUser(withEmail: email, password: password, completion: {
(user, error) in
if error != nil {
print(error ?? "ERROR OCCURED")
self.mainActivityIndicator.stopAnimating()
return
}
let ref = Database.database().reference()
ref.child("~/users/\(user?.uid ?? "0")/username").setValue(username.lowercased())
ref.child("~/users/\(user?.uid ?? "0")/fullname").setValue(fullname)
ref.child("usernames/\(user?.uid ?? "0")").setValue(username.lowercased())
self.mainActivityIndicator.stopAnimating()
})
}
})
答案 0 :(得分:1)
Database.database().reference().child("usernames").observe
调用根本不会调用with
块。我会假设在观察到事件.childAdded
时调用了块,但我看不到会添加子代码的代码。对我来说,这看起来像是在设置一个异步观察者,所以当你进行调用时,这段代码不会运行,它会在被监视的事件发生时运行。