From c76fa44e90e65cbd271351bf92b56a3cb15bfac6 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Tue, 30 Jan 2024 13:42:02 +0000 Subject: [PATCH] Fix A11yManager initialization warning and Race Condition --- .../react-native/React/Modules/RCTUIManager.m | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/react-native/React/Modules/RCTUIManager.m b/packages/react-native/React/Modules/RCTUIManager.m index 1a29de0009d811..4109067d13e36c 100644 --- a/packages/react-native/React/Modules/RCTUIManager.m +++ b/packages/react-native/React/Modules/RCTUIManager.m @@ -179,15 +179,20 @@ - (void)setBridge:(RCTBridge *)bridge _componentDataByName[componentData.name] = componentData; } } - + // Preload the a11yManager as the RCTUIManager needs it to listen for notification + // By eagerly preloading it in the setBridge method, we make sure that the manager is + // properly initialized in the Main Thread and that we do not incur in any race condition + // or concurrency problem. + id a11yManager = [self->_bridge moduleForName:@"AccessibilityManager" + lazilyLoadIfNecessary:YES]; + __weak NSObject * a11yManagerWeakObject = a11yManager; // This dispatch_async avoids a deadlock while configuring native modules - dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{ - id a11yManager = [self->_bridge moduleForName:@"AccessibilityManager" - lazilyLoadIfNecessary:YES]; + dispatch_async(dispatch_get_main_queue(), ^{ + __strong NSObject * a11yManagerStrongObject = a11yManagerWeakObject; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNewContentSizeMultiplier) name:@"RCTAccessibilityManagerDidUpdateMultiplierNotification" - object:a11yManager]; + object:a11yManagerStrongObject]; }); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(namedOrientationDidChange)