Skip to content

Commit 23717e4

Browse files
appdenfacebook-github-bot
authored andcommitted
Call stopObserving on correct queue
Summary: Since `dealloc` can be called from any thread, this would result `stopObserving` being called on a different thread/queue than the specified `methodQueue`. We specifically encountered this issue with a module needing the main queue having its `stopObserving` called on a background queue. Changelog: [iOS][Fixed] - Call [RCTEventEmitter stopObserving] on specified method queue Reviewed By: RSNara Differential Revision: D23821741 fbshipit-source-id: 693c3be6876f863da6dd214a829af2cc13a09c3f
1 parent 18f29db commit 23717e4

9 files changed

+13
-9
lines changed

Libraries/NativeAnimation/RCTNativeAnimatedModule.mm

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ - (instancetype)init
4646

4747
- (void)invalidate
4848
{
49+
[super invalidate];
4950
[_nodesManager stopAnimationLoop];
5051
[self.bridge.eventDispatcher removeDispatchObserver:self];
5152
[self.bridge.uiManager.observerCoordinator removeObserver:self];

Libraries/NativeAnimation/RCTNativeAnimatedTurboModule.mm

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ - (instancetype)init
4747

4848
- (void)invalidate
4949
{
50+
[super invalidate];
5051
[_nodesManager stopAnimationLoop];
5152
[self.bridge.eventDispatcher removeDispatchObserver:self];
5253
[self.bridge.uiManager.observerCoordinator removeObserver:self];

Libraries/Network/RCTNetworking.mm

+3-1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ - (instancetype)initWithHandlersProvider:(NSArray<id<RCTURLRequestHandler>> * (^
168168

169169
- (void)invalidate
170170
{
171+
[super invalidate];
172+
171173
for (NSNumber *requestID in _tasksByRequestID) {
172174
[_tasksByRequestID[requestID] cancel];
173175
}
@@ -680,7 +682,7 @@ - (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request completionBlo
680682
@"timeout": @(query.timeout()),
681683
@"withCredentials": @(query.withCredentials()),
682684
};
683-
685+
684686
// TODO: buildRequest returns a cancellation block, but there's currently
685687
// no way to invoke it, if, for example the request is cancelled while
686688
// loading a large file to build the request body

React/CoreModules/RCTAppState.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
#import <React/RCTEventEmitter.h>
99

10-
@interface RCTAppState : RCTEventEmitter <RCTInvalidating>
10+
@interface RCTAppState : RCTEventEmitter
1111

1212
@end

React/CoreModules/RCTAppState.mm

-5
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ - (void)stopObserving
9999
[[NSNotificationCenter defaultCenter] removeObserver:self];
100100
}
101101

102-
- (void)invalidate
103-
{
104-
[self stopObserving];
105-
}
106-
107102
#pragma mark - App Notification Methods
108103

109104
- (void)handleMemoryWarning

React/CoreModules/RCTDevSettings.mm

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ - (dispatch_queue_t)methodQueue
202202

203203
- (void)invalidate
204204
{
205+
[super invalidate];
205206
#if ENABLE_PACKAGER_CONNECTION
206207
[[RCTPackagerConnection sharedPackagerConnection] removeHandler:_reloadToken];
207208
#endif

React/CoreModules/RCTWebSocketModule.mm

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ - (NSArray *)supportedEvents
5353

5454
- (void)invalidate
5555
{
56+
[super invalidate];
57+
5658
_contentHandlers = nil;
5759
for (RCTSRWebSocket *socket in _sockets.allValues) {
5860
socket.delegate = nil;

React/Modules/RCTEventEmitter.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* RCTEventEmitter is an abstract base class to be used for modules that emit
1313
* events to be observed by JS.
1414
*/
15-
@interface RCTEventEmitter : NSObject <RCTBridgeModule, RCTJSInvokerModule>
15+
@interface RCTEventEmitter : NSObject <RCTBridgeModule, RCTJSInvokerModule, RCTInvalidating>
1616

1717
@property (nonatomic, weak) RCTBridge *bridge;
1818

@@ -37,6 +37,8 @@
3737
- (void)startObserving;
3838
- (void)stopObserving;
3939

40+
- (void)invalidate NS_REQUIRES_SUPER;
41+
4042
- (void)addListener:(NSString *)eventName;
4143
- (void)removeListeners:(double)count;
4244

React/Modules/RCTEventEmitter.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ - (void)stopObserving
7878
// Does nothing
7979
}
8080

81-
- (void)dealloc
81+
- (void)invalidate
8282
{
8383
if (_listenerCount > 0) {
8484
[self stopObserving];

0 commit comments

Comments
 (0)