Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 28479

Handling loss of internet connectivity on React Native

$
0
0

I've made a React Native App that scans for BLE devices using a foreground service on Android. The issue I have is dealing with the loss of internet connectivity. Once the internet is available, the pending actions added to the queue when the internet was not available are not executed.

Once a beacon is detected I send an event to React Native using NativeModules, Upon receiving the event I check if the internet is available or not like this. If it's not it's stored in a queue.

    const didEnterRegion = async (store, beacons) => {
      const { isConnected } = store.getState().connection;

      if (isConnected) {
        store.dispatch({
          type: REQUEST_CHECKIN,
          payload: beacons,
        });
      } else {
        NativeModules.BeaconFinder.sendNotification(
          'Looks like you entered a venue, but the internet is not reachable.',
          beacons.uuid.toString(),
        );
        store.dispatch({
          type: ADD_ACTION_TO_QUEUE,
          name: REQUEST_CHECKIN,
          payload: beacons,
        });
      }
    };

In App.js I constantly check for internet connectivity. If it's available I execute all actions in queue.

    export default class App extends Component {
      componentDidMount() {
        const unsubscribe = NetInfo.addEventListener(state => {
          this._handleConnectionChange(state.isInternetReachable);
        });
      }

      componentWillUnmount() {
        //unsubscribe();
      }

      _handleConnectionChange = isConnected => {
        if (isConnected && _.size(store.getState().connection.actionQueue) > 0) {
          let queue = store.getState().connection.actionQueue;

          _.map(queue, action => {
            console.log(action);
            store.dispatch({
              type: action.name,
              payload: action.payload
            });
          });
        }
        store.dispatch({
          type: CHANGE_CONNECTION_STATUS,
          isConnected
        });
      };
      render...
}

This works when the app is in the foreground, but when the app is killed from the task switcher, the code executes randomly i.e. It works sometimes and doesn't work sometimes.


Viewing all articles
Browse latest Browse all 28479

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>