Skip to content

Commit

Permalink
Register RCTDeviceInfo to invalidating and cleanup observer (#42396)
Browse files Browse the repository at this point in the history
Summary:

Cmmunity reported [#42120](#42120) where React Native was crashing if RCTDeviceInfo native module was receiving a notification while the bridge is invalidating.

Upon investigation, I realized that:
1. The RCTDeviceInfo module is never invalidated
2. Observers are still observing even when the Bridge is in an invalidated state and it is not back up.

This change makes sure that we invalidate the `RCTDeviceInfo.mm` module and that we unregister the observers.

## Changelog:
[iOS][Fixed] - Make `RCTDeviceInfo` listen to invalidate events and unregister observers while invalidating the bridge

Differential Revision: D52912604
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Jan 22, 2024
1 parent 5e8ce6c commit 5ff857c
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/react-native/React/CoreModules/RCTDeviceInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,39 @@ - (void)initialize
selector:@selector(interfaceFrameDidChange)
name:RCTWindowFrameDidChangeNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(invalidate)
name:RCTBridgeWillInvalidateModulesNotification
object:nil];
}

- (void)invalidate
{
_invalidated = YES;
[self _cleanupObservers];
}

- (void)_cleanupObservers
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:RCTAccessibilityManagerDidUpdateMultiplierNotification
object:[_moduleRegistry moduleForName:"AccessibilityManager"]];

[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:RCTUserInterfaceStyleDidChangeNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:RCTWindowFrameDidChangeNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(invalidate)
name:RCTBridgeWillInvalidateModulesNotification
object:nil];
}

static BOOL RCTIsIPhoneNotched()
Expand Down

0 comments on commit 5ff857c

Please sign in to comment.