From a67740da4e92a4697c572a5ccf7ae54614f1d711 Mon Sep 17 00:00:00 2001 From: Karan Thakkar Date: Wed, 12 Sep 2018 13:59:21 -0700 Subject: [PATCH] fix RCTNetInfo first time connection status (#20820) Summary: Fixes #20804, #8615, https://github.com/facebook/react-native/issues/18368#issuecomment-400610022 Pull Request resolved: https://github.com/facebook/react-native/pull/20820 Differential Revision: D9798488 Pulled By: hramos fbshipit-source-id: bd93a857b622edfbefdbd1baea746f27658f1366 --- Libraries/Network/RCTNetInfo.m | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Libraries/Network/RCTNetInfo.m b/Libraries/Network/RCTNetInfo.m index eedd7747e97e7f..0b3578fe0c33a2 100644 --- a/Libraries/Network/RCTNetInfo.m +++ b/Libraries/Network/RCTNetInfo.m @@ -36,12 +36,14 @@ @implementation RCTNetInfo { + SCNetworkReachabilityRef _firstTimeReachability; SCNetworkReachabilityRef _reachability; NSString *_connectionType; NSString *_effectiveConnectionType; NSString *_statusDeprecated; NSString *_host; BOOL _isObserving; + RCTPromiseResolveBlock _resolve; } RCT_EXPORT_MODULE() @@ -49,7 +51,18 @@ @implementation RCTNetInfo 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}]; @@ -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