简体   繁体   中英

React Native Background Job Scheduler

I need to get a solution for Background job schedule . My purpose is to send latitude and longitude to my back end in every 15 minutes. What is the best way to achieve this functionality?

I had the exact specification months ago and I concluded with 2 approach.

While my app requires active tracking and dispatch latlong object to my backend whenever there is a new latlong object coming in, one of the best options you can look for is HeadlessJS , some native coding is required.

My solution

All Javascript execution takes place on a background thread

Separate your business logic away from UI Thread and store them in redux. As long as the app is alive, it is entirely possible to dispatch an action every n minutes. Make sure you turn on the background mode in the Capabilities panel (for iOS).

Another options that I've tried

BackgroundTimer allows you to execute a particular snippet every n minutes. For example,

export const watchGPSPositionsAtInterval = function(
  dispatchNewPositionFunction,
  dispatch
) {
  let intervalId = BackgroundTimer.setInterval(() => {
    navigator.geolocation.getCurrentPosition(
      pos => {
        if (pos <= MIN_ACCURACY) {
          dispatchNewPositionFunction(pos, dispatch);
        }
      },
      error => {
        console.log(error);
      },
      {
        enableHighAccuracy: true,
        timeout,
        maximumAge
      }
    );
  }, TIME_INTERVAL);
  return intervalId;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM