您必须自己使用异步完成处理程序,并验证是否存在Internet连接:
func checkUsernameAlreadyTaken(username: String, completionHandler: (Bool) -> ()) { databaseRef.child("usernames").child("(username)").observe(.value, with: { (snapshot) in print(username) if snapshot.exists() { completionHandler(false) } else { let connectedRef = FIRDatabase.database().reference(withPath: ".info/connected") connectedRef.observe(.value, with: { snapshot in if let connected = snapshot.value as? Bool, connected { completionHandler(true) } else { completionHandler(false) // Show a popup with the description let alert = UIalertController(title: NSLocalizedString("No connection", comment: "Title Internet connection error"), message: NSLocalizedString("No internet connection, please go online", comment: "Internet connection error saving/retriving data in Firebase Database"), preferredStyle: .alert) let defaultOkAction = UIalertAction(title: NSLocalizedString("No internet connection, please go online", comment: "Internet connection error saving/retriving data in Firebase Database"), style: .default, handler: nil) alert.addAction(defaultOkAction) self.present(alert, animated: true, completion: nil) } }) } })}然后,使用以下命令调用方法:
checkIfUserExists(username: text, completionHandler: { (value) in // ...})


