Skip to content

Commit

Permalink
fix RCTNetInfo first time connection status (facebook#20820)
Browse files Browse the repository at this point in the history
Summary:
Fixes facebook#20804, facebook#8615, facebook#18368 (comment)
Pull Request resolved: facebook#20820

Differential Revision: D9798488

Pulled By: hramos

fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366
  • Loading branch information
karanjthakkar authored and facebook-github-bot committed Sep 12, 2018
1 parent 616bd1d commit a67740d
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions Libraries/Network/RCTNetInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,33 @@

@implementation RCTNetInfo
{
SCNetworkReachabilityRef _firstTimeReachability;
SCNetworkReachabilityRef _reachability;
NSString *_connectionType;
NSString *_effectiveConnectionType;
NSString *_statusDeprecated;
NSString *_host;
BOOL _isObserving;
RCTPromiseResolveBlock _resolve;
}

RCT_EXPORT_MODULE()

static void RCTReachabilityCallback(__unused SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info)
{
RCTNetInfo *self = (__bridge id)info;
if ([self setReachabilityStatus:flags] && self->_isObserving) {
BOOL didSetReachabilityFlags = [self setReachabilityStatus:flags];
if (self->_firstTimeReachability && self->_resolve) {
SCNetworkReachabilityUnscheduleFromRunLoop(self->_firstTimeReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
CFRelease(self->_firstTimeReachability);
self->_resolve(@{@"connectionType": self->_connectionType ?: RCTConnectionTypeUnknown,
@"effectiveConnectionType": self->_effectiveConnectionType ?: RCTEffectiveConnectionTypeUnknown,
@"network_info": self->_statusDeprecated ?: RCTReachabilityStateUnknown});
self->_firstTimeReachability = nil;
self->_resolve = nil;
}

if (didSetReachabilityFlags && self->_isObserving) {
[self sendEventWithName:@"networkStatusDidChange" body:@{@"connectionType": self->_connectionType,
@"effectiveConnectionType": self->_effectiveConnectionType,
@"network_info": self->_statusDeprecated}];
Expand Down Expand Up @@ -163,12 +176,8 @@ - (BOOL)setReachabilityStatus:(SCNetworkReachabilityFlags)flags
RCT_EXPORT_METHOD(getCurrentConnectivity:(RCTPromiseResolveBlock)resolve
reject:(__unused RCTPromiseRejectBlock)reject)
{
SCNetworkReachabilityRef reachability = [self getReachabilityRef];
SCNetworkReachabilityUnscheduleFromRunLoop(reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
CFRelease(reachability);
resolve(@{@"connectionType": _connectionType ?: RCTConnectionTypeUnknown,
@"effectiveConnectionType": _effectiveConnectionType ?: RCTEffectiveConnectionTypeUnknown,
@"network_info": _statusDeprecated ?: RCTReachabilityStateUnknown});
_firstTimeReachability = [self getReachabilityRef];
_resolve = resolve;
}

@end

0 comments on commit a67740d

Please sign in to comment.