From 8d2eec367dd6fbd60792ca1bde12b875a8261fa6 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Fri, 7 Jul 2023 03:46:53 -0700 Subject: [PATCH] Update when view are added to the ViewRegistry (#38223) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38223 Before this change, the InteropLayer was adding the view to the registry right before invoking a command and right after the command was invoked. This model was too strict as some view commands may have async behaviors that make the lookup in the registry fail. After this change, we are going to register the view when it's created and we are going to remove it when the view is deallocated. ## Changelog: [iOS][Changed] - Update logic to add and remove views in the view registry for the interop layer. Reviewed By: sammy-SC Differential Revision: D47262664 fbshipit-source-id: 503f4e29e03bfc7ad861c1502129822b383ffcc0 --- .../RCTLegacyViewManagerInteropCoordinatorAdapter.mm | 2 ++ .../RCTLegacyViewManagerInteropCoordinator.h | 4 ++++ .../RCTLegacyViewManagerInteropCoordinator.mm | 9 ++++----- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropCoordinatorAdapter.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropCoordinatorAdapter.mm index 951db3af44f028..0fea1bfc95afbc 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropCoordinatorAdapter.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropCoordinatorAdapter.mm @@ -24,6 +24,7 @@ - (instancetype)initWithCoordinator:(RCTLegacyViewManagerInteropCoordinator *)co - (void)dealloc { + [_coordinator removeViewFromRegistryWithTag:_tag]; [_paperView removeFromSuperview]; [_coordinator removeObserveForTag:_tag]; } @@ -39,6 +40,7 @@ - (UIView *)paperView weakSelf.eventInterceptor(eventName, event); } }]; + [_coordinator addViewToRegistry:_paperView withTag:_tag]; } return _paperView; } diff --git a/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/RCTLegacyViewManagerInteropCoordinator.h b/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/RCTLegacyViewManagerInteropCoordinator.h index 0e226c6e90c8b6..05e8a977b97360 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/RCTLegacyViewManagerInteropCoordinator.h +++ b/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/RCTLegacyViewManagerInteropCoordinator.h @@ -38,6 +38,10 @@ typedef void (^InterceptorBlock)(std::string eventName, folly::dynamic event); reactTag:(NSInteger)tag paperView:(UIView *)paperView; +- (void)removeViewFromRegistryWithTag:(NSInteger)tag; + +- (void)addViewToRegistry:(UIView *)view withTag:(NSInteger)tag; + @end NS_ASSUME_NONNULL_END diff --git a/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/RCTLegacyViewManagerInteropCoordinator.mm b/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/RCTLegacyViewManagerInteropCoordinator.mm index 5b22004daf1cc0..d1b0bfff4fcb9f 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/RCTLegacyViewManagerInteropCoordinator.mm +++ b/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/RCTLegacyViewManagerInteropCoordinator.mm @@ -131,22 +131,19 @@ - (void)handleCommand:(NSString *)commandName NSArray *newArgs = [@[ [NSNumber numberWithInteger:tag] ] arrayByAddingObjectsFromArray:args]; if (_bridge) { - [self _addViewToRegistry:paperView withTag:tag]; [_bridge.batchedBridge dispatchBlock:^{ [method invokeWithBridge:self->_bridge module:self->_componentData.manager arguments:newArgs]; [self->_bridge.uiManager setNeedsLayout]; } queue:RCTGetUIManagerQueue()]; - [self _removeViewFromRegistryWithTag:tag]; } else { // TODO T86826778 - Figure out which queue this should be dispatched to. [method invokeWithBridge:nil module:self->_componentData.manager arguments:newArgs]; } } -#pragma mark - Private -- (void)_addViewToRegistry:(UIView *)view withTag:(NSInteger)tag +- (void)addViewToRegistry:(UIView *)view withTag:(NSInteger)tag { [self _addUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) { if ([viewRegistry objectForKey:@(tag)] != NULL) { @@ -158,7 +155,7 @@ - (void)_addViewToRegistry:(UIView *)view withTag:(NSInteger)tag }]; } -- (void)_removeViewFromRegistryWithTag:(NSInteger)tag +- (void)removeViewFromRegistryWithTag:(NSInteger)tag { [self _addUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) { if ([viewRegistry objectForKey:@(tag)] == NULL) { @@ -171,6 +168,8 @@ - (void)_removeViewFromRegistryWithTag:(NSInteger)tag }]; } +#pragma mark - Private + - (void)_addUIBlock:(RCTViewManagerUIBlock)block { __weak __typeof__(self) weakSelf = self;