From 279fb52e033daba60393e400e1ee585e7d067090 Mon Sep 17 00:00:00 2001 From: wood1986 <5212215+wood1986@users.noreply.github.com> Date: Tue, 3 Jan 2023 11:41:40 -0800 Subject: [PATCH] fix: fix the potential race condition when dismissing and presentating modal (#35705) Summary: `react-native-screens` has a bug about `UIViewControllerHierarchyInconsistency` when dismissing a `react-native` ``. Here is the bug https://github.com/software-mansion/react-native-screens/issues/944 After adding `dispatch_async` block, it solves the issue. But I do not know if this is right. Here is the example repo https://github.com/wood1986/react-native-modal-crash You can revert my last commit https://github.com/wood1986/react-native-modal-crash/commit/86e7bc1adf444c1645c0d6078b0d6d427cdf6a36 to reproduce the issue ## Changelog [IOS] [FIXED] - fixed the potential race condition when dismissing and presentating modal Pull Request resolved: https://github.com/facebook/react-native/pull/35705 Reviewed By: cipolleschi Differential Revision: D42253488 Pulled By: makovkastar fbshipit-source-id: 3e98fa9e719ecdeddeb2a367b0cd364e15136d56 --- 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