I'm currently trying to implement some functions in my own application, where I use information stored locally if the device is offline, and select if the device is connected to the network.
I used NetInfo after reading this How to deal with network failure in React-Native when the network is disconnected , but unfortunately I encountered an error when NetInfo always returns offline, I found this github problem that recommended me change the host in RCTReachability. m from 'htpp: //apple.com' to 'apple.com ". However, I could not find the file with this name in the project directory. Instead, I found the only mention of" apple.com "in any file that was in RCTNetInfo.m, which was in the correct form.
Does anyone know how to solve this problem? Or maybe another way to perform one action if the device is connected to the network, and another if the device is disconnected?
Here is the relevant code:
fetchData() {
NetInfo.isConnected.fetch().done((isConnected) => {
console.log('First, is ' + (isConnected ? 'online' : 'offline'));
if ( isConnected )
{
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
store.save('contacts', responseData.feed.entry)
.then(() => store.get('contacts'))
.then((contacts) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(contacts),
isLoading: false
});
})
})
.catch((error) => { console.error(error); });
}
else
{
store.get('contacts')
.then(contacts => {
if (contacts == null)
{
this.setState({
dataSource: this.state.dataSource.cloneWithRows(CONTACT_DATA),
isLoading: false
});
}
else
{
this.setState({
dataSource: this.state.dataSource.cloneWithRows(contacts),
isLoading: false
});
}
})
}
});
}