From 46591f1012ecaee820eb6995f6cfb0018321376a Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Mon, 2 Oct 2023 11:36:42 -0700 Subject: [PATCH] dispatch status bar methods onto main queue (#39759) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39759 Changelog: [Internal] as part of the sync void tm methods test, there are some modules that do not behave correctly when trying to execute their methods on the js thread. this module accesses UIKit, so we explicitly dispatch async to the main thread Differential Revision: D49835587 fbshipit-source-id: 9881dc7017c3264bc4f808023030b8e0b085f77b --- .../React/CoreModules/RCTStatusBarManager.mm | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTStatusBarManager.mm b/packages/react-native/React/CoreModules/RCTStatusBarManager.mm index 5d5318fb4148c7..29309135309eac 100644 --- a/packages/react-native/React/CoreModules/RCTStatusBarManager.mm +++ b/packages/react-native/React/CoreModules/RCTStatusBarManager.mm @@ -92,11 +92,6 @@ - (void)stopObserving [[NSNotificationCenter defaultCenter] removeObserver:self]; } -- (dispatch_queue_t)methodQueue -{ - return dispatch_get_main_queue(); -} - - (void)emitEvent:(NSString *)eventName forNotification:(NSNotification *)notification { CGRect frame = [notification.userInfo[UIApplicationStatusBarFrameUserInfoKey] CGRectValue]; @@ -130,35 +125,41 @@ - (void)applicationWillChangeStatusBarFrame:(NSNotification *)notification RCT_EXPORT_METHOD(setStyle : (NSString *)style animated : (BOOL)animated) { - UIStatusBarStyle statusBarStyle = [RCTConvert UIStatusBarStyle:style]; - if (RCTViewControllerBasedStatusBarAppearance()) { - RCTLogError(@"RCTStatusBarManager module requires that the \ + dispatch_async(dispatch_get_main_queue(), ^{ + UIStatusBarStyle statusBarStyle = [RCTConvert UIStatusBarStyle:style]; + if (RCTViewControllerBasedStatusBarAppearance()) { + RCTLogError(@"RCTStatusBarManager module requires that the \ UIViewControllerBasedStatusBarAppearance key in the Info.plist is set to NO"); - } else { + } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [RCTSharedApplication() setStatusBarStyle:statusBarStyle animated:animated]; - } + [RCTSharedApplication() setStatusBarStyle:statusBarStyle animated:animated]; + } #pragma clang diagnostic pop + }); } RCT_EXPORT_METHOD(setHidden : (BOOL)hidden withAnimation : (NSString *)withAnimation) { - UIStatusBarAnimation animation = [RCTConvert UIStatusBarAnimation:withAnimation]; - if (RCTViewControllerBasedStatusBarAppearance()) { - RCTLogError(@"RCTStatusBarManager module requires that the \ + dispatch_async(dispatch_get_main_queue(), ^{ + UIStatusBarAnimation animation = [RCTConvert UIStatusBarAnimation:withAnimation]; + if (RCTViewControllerBasedStatusBarAppearance()) { + RCTLogError(@"RCTStatusBarManager module requires that the \ UIViewControllerBasedStatusBarAppearance key in the Info.plist is set to NO"); - } else { + } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [RCTSharedApplication() setStatusBarHidden:hidden withAnimation:animation]; + [RCTSharedApplication() setStatusBarHidden:hidden withAnimation:animation]; #pragma clang diagnostic pop - } + } + }); } RCT_EXPORT_METHOD(setNetworkActivityIndicatorVisible : (BOOL)visible) { - RCTSharedApplication().networkActivityIndicatorVisible = visible; + dispatch_async(dispatch_get_main_queue(), ^{ + RCTSharedApplication().networkActivityIndicatorVisible = visible; + }); } - (facebook::react::ModuleConstants)getConstants