From 137628f6e9afef72667eac795b6ff019d97a7bb0 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Tue, 30 Jan 2024 05:43:09 -0800 Subject: [PATCH] Fix warning when loading RCTUIManager and A11yManager (#42733) Summary: When we fixed the race condition between A11yManager and RCTUIManager, we did it by moving the A11yManager on a background queue. In the old architecture, this was raising a warning which our users might find confusing. Plus, that change was not aligned with what the A11yManager declared in its configuration because we are actually initializing it starting from a BG queue. {F1405693310} With this change we anticipate the initialization of the module in a place where: 1. We know we are in the main queue 2. We know we are going to need it (so it is not violating the lazy load principle) 3. We know it is safe. This should allow us to also remove the feature flag of `RCTUIManagerDispatchAccessibilityManagerInitOntoMain` because now it is safe to use the main_queue as requested by the module. ## Changelog: [iOS][Fixed] - Initialize the A11yManager in the main queue and when we need it. Differential Revision: D53225120 --- packages/react-native/React/Modules/RCTUIManager.m | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/react-native/React/Modules/RCTUIManager.m b/packages/react-native/React/Modules/RCTUIManager.m index fca5ee7aa90072..b4522a0fcdc734 100644 --- a/packages/react-native/React/Modules/RCTUIManager.m +++ b/packages/react-native/React/Modules/RCTUIManager.m @@ -180,16 +180,23 @@ - (void)setBridge:(RCTBridge *)bridge } } + // 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_queue_t accessibilityManagerInitQueue = RCTUIManagerDispatchAccessibilityManagerInitOntoMain() ? dispatch_get_main_queue() : dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0); dispatch_async(accessibilityManagerInitQueue, ^{ + __strong NSObject *a11yManagerStrongObject = a11yManagerWeakObject; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNewContentSizeMultiplier) name:@"RCTAccessibilityManagerDidUpdateMultiplierNotification" - object:[self->_bridge moduleForName:@"AccessibilityManager" - lazilyLoadIfNecessary:YES]]; + object:a11yManagerStrongObject]; }); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(namedOrientationDidChange)