title: Handling loss of internet connectivity on React Native
body: <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>    const didEnterRegion = async (store, beacons) =&gt; {
        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,
          });
        }
      };
  </code></pre>
  
  <p>In App.js I constantly check for internet connectivity. If it's available I execute all actions in queue.</p>
  
  <pre><code>    export default class App extends Component {
        componentDidMount() {
          const unsubscribe = NetInfo.addEventListener(state =&gt; {
            this._handleConnectionChange(state.isInternetReachable);
          });
        }
  
        componentWillUnmount() {
          //unsubscribe();
        }
  
        _handleConnectionChange = isConnected =&gt; {
          if (isConnected &amp;&amp; _.size(store.getState().connection.actionQueue) &gt; 0) {
            let queue = store.getState().connection.actionQueue;
  
            _.map(queue, action =&gt; {
              console.log(action);
              store.dispatch({
                type: action.name,
                payload: action.payload
              });
            });
          }
          store.dispatch({
            type: CHANGE_CONNECTION_STATUS,
            isConnected
          });
        };
        render...
  }
  </code></pre>
  
  <p>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.</p>