Platforms
This issue is related to iOS.
Versions
- iOS: 12.4.1
- react-native-netinfo: "^5.5.0"
- react-native: "0.61.5"
- react: "16.9.0"
Description
I'm working on implementing network status check in a project with dynamic data and checking for internet reachability whenever an API call is made or the app state changes. My issue is that the key isInternetReachable is always returning false for a minute before returning the correct status whenever app comes to foreground from background even though internet is reachable. The issue is happening on iOS real device and everything is working fine on iOS simulator and android real device. Can someone help me please. Thanks in advance.
Reproducible Demo
let state;
componentDidMount() {
// Adding AppState event listener
AppState.addEventListener('change', this._handleAppStateChange);
// Adding NetInfo event listener
this._subscription = NetInfo.addEventListener(
this._handleConnectionInfoChange,
);
}
componentWillUnmount() {
// Removing AppState event listener
AppState.removeEventListener('change', this._handleAppStateChange);
// Removing NetInfo event listener
this._subscription && this._subscription();
}
_handleAppStateChange = async nextAppState => {
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
state = await NetInfo.fetch();
});
}
if (this.state.appState.match(/active|inactive/) && nextAppState === 'background') {
state = await NetInfo.fetch();
});
}
if (this.state.appState.match(/active|background/) && nextAppState === 'inactive') {
state = await NetInfo.fetch();
});
}
this.setState({ appState: nextAppState });
};
getJobs = async (index) => {
// fetching status of internet connection
state = await NetInfo.fetch();
if (state.isInternetReachable === false) {
Alert.alert(Constants.k_APP_NAME, Constants.k_OFFLINE_TEXT);
} else {
this.props.callService(item, false);
}
}
}