Swifter Swifter - 5 months ago 18
Swift Question

Reset Password Issue with Firebase and Swift

After Firebase updated their backend, I'm having issues with reset password. For some reason the app crashes (even though it sends emails with the reset password). Here's the console error that I get:

2016-06-04 12:32:21.883 NewApp[47459:27055361] *** Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/Keyboard/UIKeyboardTaskQueue.m:386
2016-06-04 12:32:21.890 NewApp[47459:27055361] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
libc++abi.dylib: terminating with uncaught exception of type NSException


In the ResetPasswordVC I have the following:

@IBAction func resetPasswordTapped(sender: ButtonWhite) {

SVProgressHUD.showWithStatus("Please, wait...")
SVProgressHUD.setDefaultMaskType(.Gradient)

let email = emailTextField.text

if email != "" {

FIRAuth.auth()?.sendPasswordResetWithEmail(email!, completion: { (error) in

if error != nil {

// Error - Unidentified Email
SVProgressHUD.dismiss()
showAlert(title: "Unidentified Email Address", msg: "Please, re-enter the email you have registered with.", actionButton: "OK", viewController: self)

} else {

// Success - Sent recovery email

SVProgressHUD.dismiss()

let alertController = UIAlertController(title: "Email Sent", message: "An email has been sent. Please, check your email now.", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in

self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alertController, animated: true, completion: nil)
}

})

} else {

SVProgressHUD.dismiss()
showAlert(title: "Error!", msg: "Email is required in order to reset your password. Please, enter your email. ", actionButton: "OK", viewController: self)
}
}


I've tested it with breakpoints to see the flow, it reaches
FIRAuth.auth()?.sendPasswordResetWithEmail(email!, completion: { (error) in
where the email has a value and it should perform some checks based on it if/else statements but it doesnt.

I have no idea why. The code is working (sends password recovery emails) but the app crashes with that error. Clues?

Answer

I figured out the issue and the solution. For some reason the issue's within the threads. This solved it:

            FIRAuth.auth()?.sendPasswordResetWithEmail(email!, completion: { (error) in


            NSOperationQueue.mainQueue().addOperationWithBlock {

            if error != nil {

                // Error - Unidentified Email
                SVProgressHUD.dismiss()
                showAlert(title: "Unidentified Email Address", msg: "Please, re-enter the email you have registered with.", actionButton: "OK", viewController: self)

            } else {

                // Success - Sends recovery email

                SVProgressHUD.dismiss()

                let alertController = UIAlertController(title: "Email Sent", message: "An email has been sent. Please, check your email now.", preferredStyle: .Alert)
                alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in

                    self.dismissViewControllerAnimated(true, completion: nil)
                }))
                self.presentViewController(alertController, animated: true, completion: nil)
            }

            }})