您必须自己使用异步完成处理程序,并验证是否存在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
// ...
})