Skip to content

Commit

Permalink
fix: fix the potential race condition when dismissing and presentatin…
Browse files Browse the repository at this point in the history
…g 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
  • Loading branch information
wood1986 authored and Riccardo Cipolleschi committed Jan 30, 2023
1 parent b5280bb commit 279fb52
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions React/Views/RCTModalHostViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 279fb52

Please sign in to comment.