Skip to content

Commit 870ce57

Browse files
wood1986kelset
authored andcommitted
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` `<Modal>`. Here is the bug software-mansion/react-native-screens#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 wood1986/react-native-modal-crash@86e7bc1 to reproduce the issue ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [IOS] [FIXED] - fixed the potential race condition when dismissing and presentating modal Pull Request resolved: #35705 Reviewed By: cipolleschi Differential Revision: D42253488 Pulled By: makovkastar fbshipit-source-id: 3e98fa9e719ecdeddeb2a367b0cd364e15136d56
1 parent c6bfbf9 commit 870ce57

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

React/Views/RCTModalHostViewManager.m

+16-12
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ - (void)presentModalHostView:(RCTModalHostView *)modalHostView
7575
modalHostView.onShow(nil);
7676
}
7777
};
78-
if (_presentationBlock) {
79-
_presentationBlock([modalHostView reactViewController], viewController, animated, completionBlock);
80-
} else {
81-
[[modalHostView reactViewController] presentViewController:viewController
82-
animated:animated
83-
completion:completionBlock];
84-
}
78+
dispatch_async(dispatch_get_main_queue(), ^{
79+
if (self->_presentationBlock) {
80+
self->_presentationBlock([modalHostView reactViewController], viewController, animated, completionBlock);
81+
} else {
82+
[[modalHostView reactViewController] presentViewController:viewController
83+
animated:animated
84+
completion:completionBlock];
85+
}
86+
});
8587
}
8688

8789
- (void)dismissModalHostView:(RCTModalHostView *)modalHostView
@@ -93,11 +95,13 @@ - (void)dismissModalHostView:(RCTModalHostView *)modalHostView
9395
[[self.bridge moduleForClass:[RCTModalManager class]] modalDismissed:modalHostView.identifier];
9496
}
9597
};
96-
if (_dismissalBlock) {
97-
_dismissalBlock([modalHostView reactViewController], viewController, animated, completionBlock);
98-
} else {
99-
[viewController.presentingViewController dismissViewControllerAnimated:animated completion:completionBlock];
100-
}
98+
dispatch_async(dispatch_get_main_queue(), ^{
99+
if (self->_dismissalBlock) {
100+
self->_dismissalBlock([modalHostView reactViewController], viewController, animated, completionBlock);
101+
} else {
102+
[viewController.presentingViewController dismissViewControllerAnimated:animated completion:completionBlock];
103+
}
104+
});
101105
}
102106

103107
- (RCTShadowView *)shadowView

0 commit comments

Comments
 (0)