From a11da0f42ee9c5f8ca54a6308f070ef9bc25393a Mon Sep 17 00:00:00 2001 From: WooD Fung Date: Thu, 22 Dec 2022 14:56:09 -0800 Subject: [PATCH] fix: fix the potential race condition when dismissing and presentating --- React/Views/RCTModalHostViewManager.m | 28 +++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/React/Views/RCTModalHostViewManager.m b/React/Views/RCTModalHostViewManager.m index 7fab70ef636368..4b9f9ad7267c8f 100644 --- a/React/Views/RCTModalHostViewManager.m +++ b/React/Views/RCTModalHostViewManager.m @@ -75,13 +75,15 @@ - (void)presentModalHostView:(RCTModalHostView *)modalHostView modalHostView.onShow(nil); } }; - if (_presentationBlock) { - _presentationBlock([modalHostView reactViewController], viewController, animated, completionBlock); - } else { - [[modalHostView reactViewController] presentViewController:viewController - animated:animated - completion:completionBlock]; - } + dispatch_async(dispatch_get_main_queue(), ^{ + if (self->_presentationBlock) { + self->_presentationBlock([modalHostView reactViewController], viewController, animated, completionBlock); + } else { + [[modalHostView reactViewController] presentViewController:viewController + animated:animated + completion:completionBlock]; + } + }); } - (void)dismissModalHostView:(RCTModalHostView *)modalHostView @@ -93,11 +95,13 @@ - (void)dismissModalHostView:(RCTModalHostView *)modalHostView [[self.bridge moduleForClass:[RCTModalManager class]] modalDismissed:modalHostView.identifier]; } }; - if (_dismissalBlock) { - _dismissalBlock([modalHostView reactViewController], viewController, animated, completionBlock); - } else { - [viewController.presentingViewController dismissViewControllerAnimated:animated completion:completionBlock]; - } + dispatch_async(dispatch_get_main_queue(), ^{ + if (self->_dismissalBlock) { + self->_dismissalBlock([modalHostView reactViewController], viewController, animated, completionBlock); + } else { + [viewController.presentingViewController dismissViewControllerAnimated:animated completion:completionBlock]; + } + }); } - (RCTShadowView *)shadowView