Skip to content

Adding temporary workaround to call FirebaseAuth completion block in … #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions FirebasePhoneAuthUI/FUIPhoneEntryViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,36 @@ - (void)onNext:(NSString *)phoneNumber {
[provider verifyPhoneNumber:phoneNumberWithCountryCode
UIDelegate:self
completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
// Temporary fix to guarantee execution of the completion block on the main thread.
// TODO: Remove temporary workaround when the issue is fixed in FirebaseAuth.
dispatch_block_t completionBlock = ^() {
[self decrementActivity];
self.navigationItem.rightBarButtonItem.enabled = YES;

if (error) {
[_phoneNumberField becomeFirstResponder];

UIAlertController *alertController = [FUIPhoneAuth alertControllerForError:error
actionHandler:nil];
[self presentViewController:alertController animated:YES completion:nil];

FUIPhoneAuth *delegate = [self.authUI providerWithID:FIRPhoneAuthProviderID];
[delegate callbackWithCredential:nil error:error result:nil];
return;
}

[self decrementActivity];
self.navigationItem.rightBarButtonItem.enabled = YES;

if (error) {
[_phoneNumberField becomeFirstResponder];
UIViewController *controller =
[[FUIPhoneVerificationViewController alloc] initWithAuthUI:self.authUI
verificationID:verificationID
phoneNumber:phoneNumberWithCountryCode];

UIAlertController *alertController = [FUIPhoneAuth alertControllerForError:error
actionHandler:nil];
[self presentViewController:alertController animated:YES completion:nil];

FUIPhoneAuth *delegate = [self.authUI providerWithID:FIRPhoneAuthProviderID];
[delegate callbackWithCredential:nil error:error result:nil];
return;
[self pushViewController:controller];
};
if ([NSThread isMainThread]) {
completionBlock();
} else {
dispatch_sync(dispatch_get_main_queue(), completionBlock);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this earlier... I think we'd better to do dispatch_async instead of dispatch_sync, otherwise if the delegate calls FIRAuth methods there is a risk of deadlock.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1eb511c

}

UIViewController *controller =
[[FUIPhoneVerificationViewController alloc] initWithAuthUI:self.authUI
verificationID:verificationID
phoneNumber:phoneNumberWithCountryCode];

[self pushViewController:controller];
}];
}

Expand Down
44 changes: 26 additions & 18 deletions FirebasePhoneAuthUI/FUIPhoneVerificationViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,33 @@ - (IBAction)onResendCode:(id)sender {
[provider verifyPhoneNumber:_phoneNumber
UIDelegate:self
completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {

[self decrementActivity];
_verificationID = verificationID;
[_codeField becomeFirstResponder];

if (error) {
UIAlertController *alertController = [FUIPhoneAuth alertControllerForError:error
actionHandler:^{
[_codeField clearCodeInput];
[_codeField becomeFirstResponder];
}];
[self presentViewController:alertController animated:YES completion:nil];
return;
// Temporary fix to guarantee execution of the completion block on the main thread.
// TODO: Remove temporary workaround when the issue is fixed in FirebaseAuth.
dispatch_block_t completionBlock = ^() {
[self decrementActivity];
_verificationID = verificationID;
[_codeField becomeFirstResponder];

if (error) {
UIAlertController *alertController = [FUIPhoneAuth alertControllerForError:error
actionHandler:^{
[_codeField clearCodeInput];
[_codeField becomeFirstResponder];
}];
[self presentViewController:alertController animated:YES completion:nil];
return;
}

NSString *resultMessage =
[NSString stringWithFormat:FUIPhoneAuthLocalizedString(kPAStr_ResendCodeResult),
_phoneNumber];
[self showAlertWithMessage:resultMessage];
};
if ([NSThread isMainThread]) {
completionBlock();
} else {
dispatch_sync(dispatch_get_main_queue(), completionBlock);
}

NSString *resultMessage =
[NSString stringWithFormat:FUIPhoneAuthLocalizedString(kPAStr_ResendCodeResult),
_phoneNumber];
[self showAlertWithMessage:resultMessage];
}];
}
- (IBAction)onPhoneNumberSelected:(id)sender {
Expand Down
2 changes: 1 addition & 1 deletion FirebaseUI_dev_auth.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Pod::Spec.new do |s|
'FirebaseAuthUI/**/*.xib']
}
authbase.dependency 'Firebase/Analytics'
authbase.dependency 'FirebaseAuth'
authbase.dependency 'FirebaseAuth', '~> 4.2'
end

end