我正在使用我的SignUpViewController创建一个帐户,当用户使用不符合密码规范的密码注册一个帐户时,该程序应发出警报。出于某种原因,我不断得到:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished]只能从主线程调用。'
如何返回主线程,以便程序仅发出警报?
这是该视图控制器的代码:
@IBAction func signupPressed(_ sender: Any) {
// Get a reference to the user pool
let userPool = AppDelegate.defaultUserPool()
// Collect all of the attributes that should be included in the signup call
let emailAttribute = AWSCognitoIdentityUserAttributeType(name: "email", value: self.email.text!)
let firstNameAttribute = AWSCognitoIdentityUserAttributeType(name: "given_name", value: self.firstName.text!)
let lastNameAttribute = AWSCognitoIdentityUserAttributeType(name: "family_name", value: self.lastName.text!)
let birthdayAttribute = AWSCognitoIdentityUserAttributeType(name: "birthdate", value: self.birthday.text!)
// Actually make the signup call passing in those attributes
userPool.signUp(UUID().uuidString, password: self.password.text!, userAttributes: [emailAttribute, firstNameAttribute, lastNameAttribute, birthdayAttribute], validationData: nil)
.continueWith { (response) -> Any? in
if response.error != nil {
// Error in the signup process
let alert = UIAlertController(title: "Error", message: response.error?.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler:nil))
self.present(alert, animated: true, completion: nil)
} else {
self.user = response.result!.user
// Does user need verification?
if (response.result?.userConfirmed?.intValue != AWSCognitoIdentityUserStatus.confirmed.rawValue) {
// User needs confirmation, so we need to proceed to the verify view controller
DispatchQueue.main.async {
self.performSegue(withIdentifier: "VerifySegue", sender: self)
}
} else {
// User signed up but does not need verification
DispatchQueue.main.async {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
}
return nil
}
}
答案 0 :(得分:3)
这也是主线程工作
DispatchQueue.main.async {
let alert = UIAlertController(title: "Error", message: response.error?.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler:nil))
self.present(alert, animated: true, completion: nil)
}
如果所有事情都需要在main中完成,那么您只能用1 DispatchQueue.main.async
包围响应
答案 1 :(得分:2)
在闭包中,如果错误不是nil,您是否可以像下面那样更新代码并重试:
if response.error != nil {
// Error in the signup process
DispatchQueue.main.async {
let alert = UIAlertController(title: "Error", message: response.error?.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler:nil))
self.present(alert, animated: true, completion: nil)
}
}
答案 2 :(得分:2)
只写
self.present(alert, animated: true, completion: nil)
在Dispatch.main.async内部,就像您对其他脚本所做的一样
DispatchQueue.main.async {
self.present(alert, animated: true, completion: nil)
}
答案 3 :(得分:0)
您已经在整理视图控制器并使用DispatchQueue.main.async
执行segue。您还应该执行此操作以显示UIAlertController
。您最好将整个方法主体编组到主线程。
答案 4 :(得分:0)
与DispatchQueue.main.async
一起,我们可以添加[weak self]
支票以避免保留周期,例如
DispatchQueue.main.async { [weak self] in
self?.present(alert, animated: true, completion: nil)
}