在我的应用程序的注册过程中,我有两个功能 - 首先检查用户输入的句柄以查看它是否已被占用,然后第二个函数设置其余的值:
@IBAction func setupDoneButtonPressed(_ sender: Any) {
checkHandle()
setUserInfo()
self.performSegue(withIdentifier: "setupToChat", sender: nil)
}
checkHandle
函数似乎正在做它的工作,因为它检查数据库然后打印“else”条件print语句 - 但是我没有看到警报,程序只是进入应用程序
如果该句柄已被使用,我需要编程停止而不是继续setUserInfo
然后进入应用程序。我想在那里显示我的警报,然后允许用户使用不同的句柄再次尝试。
这是checkHandle
函数:
func checkHandle() {
if self.handleTextField.text != nil {
if let handle = self.handleTextField.text {
let handleRef: FIRDatabaseReference = FIRDatabase.database().reference().child("users")
handleRef.queryOrdered(byChild: "handle").queryEqual(toValue: "\(handle)").observeSingleEvent(of: .value, with: { (snapshot) in
if (snapshot.value is NSNull) {
print("handle not in use") // handle not found
userRef.child("handle").setValue(handle)
} else {
print("Handle already in use. Value: \(snapshot.value)") // handle is in use
let alertController = UIAlertController(title: "Handle Taken", message: "Please choose a different handle", preferredStyle: UIAlertControllerStyle.actionSheet)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(alert :UIAlertAction!) in
})
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
})
}
}
}
如果已经存在的句柄,我该怎么做才能确保注册过程停止?
答案 0 :(得分:3)
你可以这样解决:
首先使checkHandle()
成为像checkHandle(completion: @escaping (Bool) -> Void)
然后,如果满足completion(true)
条件,则在此函数内部调用if
,如果不满足,则调用completion(false)
。然后在你的按钮处理程序中使用checkHandle()
,如下所示:
checkHandle { [weak self] success in
guard success else { return }
self?.setUserInfo()
self?.performSegue(withIdentifier: "setupToChat", sender: nil)
}
答案 1 :(得分:0)
您的代码只是继续处理而不采取任何条件行为。我建议你让checkHandle函数返回一个布尔值,然后做出相应的响应。
func checkHandle(handle: String) -> Bool {
someBool = // some logic to see if handle exists.
return someBool
}
@IBAction func setupDoneButtonPressed(_ sender: Any) {
if checkHandle() {
setUserInfo()
self.performSegue(withIdentifier: "setupToChat", sender: nil)
} else {
// present an error, or make suggestions etc
}
}
答案 2 :(得分:0)
@IBAction func setupDoneButtonPressed(_ sender: Any) {
if checkHandle()
{
setUserInfo()
self.performSegue(withIdentifier: "setupToChat", sender: nil)
}
}
func checkHandle() -> Bool {
let isAvailable = false
if self.handleTextField.text != nil {
if let handle = self.handleTextField.text {
let handleRef: FIRDatabaseReference = FIRDatabase.database().reference().child("users")
handleRef.queryOrdered(byChild: "handle").queryEqual(toValue: "\(handle)").observeSingleEvent(of: .value, with: { (snapshot) in
if (snapshot.value is NSNull) {
print("handle not in use") // handle not found
userRef.child("handle").setValue(handle)
isAvailable = true
} else {
print("Handle already in use. Value: \(snapshot.value)") // handle is in use
let alertController = UIAlertController(title: "Handle Taken", message: "Please choose a different handle", preferredStyle: UIAlertControllerStyle.actionSheet)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(alert :UIAlertAction!) in
})
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
isAvailable = false
}
})
}
}
if isAvailable
{
return true
}
return false
}