1

我正在使用我的 SignUpViewController 创建一个帐户,当用户使用不符合密码规范的密码注册帐户时,程序应该发出警报。相反,出于某种原因,我不断得到:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished] 只能从主线程调用。”

我怎样才能回到主线程,以便程序只会发出警报?

这是该视图控制器的代码:

@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
    }
}
4

5 回答 5

4

这也是一个主线程工作

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)
}

DispatchQueue.main.async顺便说一句,如果所有事情都需要在 main 中完成,那么您只能有 1来围绕响应

于 2018-12-18T21:51:42.230 回答
2

在您的闭包中,如果错误不是零,您可以更新如下代码并重试:

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)    
    }
} 
于 2018-12-18T21:52:17.627 回答
2

写吧

self.present(alert, animated: true, completion: nil)

在 Dispatch.main.async 中,就像您对其他 segues 所做的那样

DispatchQueue.main.async {
    self.present(alert, animated: true, completion: nil) 
}
于 2018-12-18T21:52:33.110 回答
0

您已经在编组呈现视图控制器并使用DispatchQueue.main.async. 您还应该这样做以显示UIAlertController. 您不妨将整个方法体编组到主线程。

于 2018-12-18T21:52:31.367 回答
0

除了DispatchQueue.main.async,我们可以添加[weak self]检查以避免保留循环,例如

DispatchQueue.main.async { [weak self] in
    self?.present(alert, animated: true, completion: nil)
}
于 2018-12-19T02:51:50.057 回答