Skip to content

Commit 4ea3ac7

Browse files
karanjthakkarkelset
authored andcommitted
fix RCTNetInfo first time connection status (#20820)
Summary: Fixes #20804, #8615, #18368 (comment) Pull Request resolved: #20820 Differential Revision: D9798488 Pulled By: hramos fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
1 parent cd091e8 commit 4ea3ac7

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

Libraries/Network/RCTNetInfo.m

+16-7
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,33 @@
3636

3737
@implementation RCTNetInfo
3838
{
39+
SCNetworkReachabilityRef _firstTimeReachability;
3940
SCNetworkReachabilityRef _reachability;
4041
NSString *_connectionType;
4142
NSString *_effectiveConnectionType;
4243
NSString *_statusDeprecated;
4344
NSString *_host;
4445
BOOL _isObserving;
46+
RCTPromiseResolveBlock _resolve;
4547
}
4648

4749
RCT_EXPORT_MODULE()
4850

4951
static void RCTReachabilityCallback(__unused SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info)
5052
{
5153
RCTNetInfo *self = (__bridge id)info;
52-
if ([self setReachabilityStatus:flags] && self->_isObserving) {
54+
BOOL didSetReachabilityFlags = [self setReachabilityStatus:flags];
55+
if (self->_firstTimeReachability && self->_resolve) {
56+
SCNetworkReachabilityUnscheduleFromRunLoop(self->_firstTimeReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
57+
CFRelease(self->_firstTimeReachability);
58+
self->_resolve(@{@"connectionType": self->_connectionType ?: RCTConnectionTypeUnknown,
59+
@"effectiveConnectionType": self->_effectiveConnectionType ?: RCTEffectiveConnectionTypeUnknown,
60+
@"network_info": self->_statusDeprecated ?: RCTReachabilityStateUnknown});
61+
self->_firstTimeReachability = nil;
62+
self->_resolve = nil;
63+
}
64+
65+
if (didSetReachabilityFlags && self->_isObserving) {
5366
[self sendEventWithName:@"networkStatusDidChange" body:@{@"connectionType": self->_connectionType,
5467
@"effectiveConnectionType": self->_effectiveConnectionType,
5568
@"network_info": self->_statusDeprecated}];
@@ -163,12 +176,8 @@ - (BOOL)setReachabilityStatus:(SCNetworkReachabilityFlags)flags
163176
RCT_EXPORT_METHOD(getCurrentConnectivity:(RCTPromiseResolveBlock)resolve
164177
reject:(__unused RCTPromiseRejectBlock)reject)
165178
{
166-
SCNetworkReachabilityRef reachability = [self getReachabilityRef];
167-
SCNetworkReachabilityUnscheduleFromRunLoop(reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
168-
CFRelease(reachability);
169-
resolve(@{@"connectionType": _connectionType ?: RCTConnectionTypeUnknown,
170-
@"effectiveConnectionType": _effectiveConnectionType ?: RCTEffectiveConnectionTypeUnknown,
171-
@"network_info": _statusDeprecated ?: RCTReachabilityStateUnknown});
179+
_firstTimeReachability = [self getReachabilityRef];
180+
_resolve = resolve;
172181
}
173182

174183
@end

0 commit comments

Comments
 (0)