函数不会调用 Swift 3
Function will not call Swift 3
@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
它根本不打印 'starting check' 或 运行 函数内部的任何代码。
这可能是一个菜鸟错误,如果这是一个愚蠢的问题,我很抱歉。
未来感谢。
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()
})
}
})
Database.database().reference().child("usernames").observe
调用根本不会调用 with
块。我会假设在观察到事件 .childAdded
时调用该块,但我看不到可以添加子项的代码。在我看来,您正在设置一个异步观察者,因此当您进行调用时,这段代码不会 运行,当监视事件发生时,它会 运行。
@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
它根本不打印 'starting check' 或 运行 函数内部的任何代码。 这可能是一个菜鸟错误,如果这是一个愚蠢的问题,我很抱歉。 未来感谢。
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()
})
}
})
Database.database().reference().child("usernames").observe
调用根本不会调用 with
块。我会假设在观察到事件 .childAdded
时调用该块,但我看不到可以添加子项的代码。在我看来,您正在设置一个异步观察者,因此当您进行调用时,这段代码不会 运行,当监视事件发生时,它会 运行。