react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

the execution time of watchPosition and the expected non-conformance

Open Simoon-F opened this issue 2 years ago • 18 comments

Your Environment

  • Plugin version: 4.12.1
  • Platform: iOS or Android : iOS
  • OS version: 16.1.1
  • Device manufacturer / model: iPhone 14
  • React Native version (react-native -v): 0.68.7
  • Plugin config
this.geolocation.ready({
      debug: false,
      maxDaysToPersist: 2,
      stationaryRadius: 100,
      stopOnTerminate: false,
      allowIdenticalLocations: false,
      desiredAccuracy: this.geolocation.DESIRED_ACCURACY_HIGH,
      notification: {
        title:
          'The delivery task starts and is using the current location to locate',
      },
      disableLocationAuthorizationAlert: true,
      locationAuthorizationRequest: 'Any',
      locationAuthorizationAlert: {
        titleWhenNotEnabled: 'Location services are not enabled',
        titleWhenOff: 'Location services are not enabled',
        instructions:
          "To ensure best app experience, please enable 'Always' location permissions in device settings.",
        cancelButton: 'Cancel',
        settingsButton: 'Settings',
      },
      backgroundPermissionRationale: {
        title: 'Update location settings',
        message:
          'Allow us to always access your location so that we can provide accurate distribution of delivery orders, delivery routes and monitor your safety, improve work efficiency',
        positiveAction: 'Change to {backgroundPermissionOptionLabel}',
        negativeAction: 'Cancel',
      },
    });

Expected Behavior

watchPosition is executed every ten seconds.

watchPosition(
      async (location: Location) => {
        console.log(
          'Trigger event log:',
          getDevicezoneDateTime('YYYY-MM-DD HH:mm:ss'),
        );

        ...
      },
      (errorCode: LocationError) => {
        ...
      },
      {
        interval: 10000,
      },
    )

Actual Behavior

Execute once in twenty seconds

The first :

 LOG  Trigger event log: 2023-08-08 17:11:09
 LOG  Trigger event log: 2023-08-08 17:11:29
 LOG  Trigger event log: 2023-08-08 17:11:49

Second time :

 LOG  Trigger event log: 2023-08-08 17:12:09
 LOG  Trigger event log: 2023-08-08 17:12:29
 LOG  Trigger event log: 2023-08-08 17:12:49

Steps to Reproduce

  1. Open the app
  2. Click button to trigger watchPosition

Context

Debug logs

Logs
PASTE_YOUR_LOGS_HERE

Simoon-F avatar Aug 09 '23 00:08 Simoon-F

What are you using .watchPosition for?

christocracy avatar Aug 09 '23 00:08 christocracy

What are you using .watchPosition for?

Timing use . watchPosition to obtain the latitude and longitude of the current device.

Simoon-F avatar Aug 09 '23 00:08 Simoon-F

Yes, that’s obvious. But why specifically .watchPosition? That method is meant for short periods of time while your app is in the foreground. You do not need to use that method. Do not use this method for long-term tracking in the background.

All you need to do to use this plug-in is call .start().

See wiki Philosophy of Operation.

christocracy avatar Aug 09 '23 00:08 christocracy

Yes, that’s obvious. But why specifically .watchPosition? That method is meant for short periods of time while your app is in the foreground. You do not need to use that method. Do not use this method for long-term tracking in the background.

All you need to do to use this plug-in is call .start().

See wiki Philosophy of Operation.

I called the start method before calling the watchPosition method.

In fact, I want to know why setting 10s execution once will become 20s.

Simoon-F avatar Aug 09 '23 00:08 Simoon-F

Why specifically are you using .watchPosition? I want people to avoid using this method.

christocracy avatar Aug 09 '23 00:08 christocracy

I have such needs here:

Whether it is the foreground or the background, record longitude and latitude once in 10 seconds and submit api once in 1 minute.

Simoon-F avatar Aug 09 '23 00:08 Simoon-F

.watchPosition is not designed for long-term use in the background.

call .changePace(true) and implement your own timer to call .getCurrentPosition

christocracy avatar Aug 09 '23 00:08 christocracy

.watchPosition is not designed for long-term use in the background.

call .changePace(true) and implement your own timer to call .getCurrentPosition

OK, thank you, I'll continue to study and see.

Simoon-F avatar Aug 09 '23 00:08 Simoon-F

@christocracy I'm facing with same issue. If I passed interval: 1000 location is triggered every ~10s, if I passed interval: 10000 location is triggered every ~20s. Looks like a bug.

Crysp avatar Oct 17 '23 14:10 Crysp

You are not providing any information (plug-in version, platform, device model, etc).

christocracy avatar Oct 17 '23 14:10 christocracy

Your Environment

  • Plugin version: 4.12.1
  • Platform: iOS
  • OS version: 16
  • Device manufacturer / model: iPhone X
  • React Native version (react-native -v): 0.72.0
  • Plugin config
{
  startOnBoot: true,
  stopOnTerminate: false,
  enableHeadless: true,
  autoSync: false,
  url: '...',
  showsBackgroundLocationIndicator: false,
  desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
  persistMode: BackgroundGeolocation.PERSIST_MODE_ALL,
  distanceFilter: 10,
  disableLocationAuthorizationAlert: true,
  preventSuspend: false,
  useSignificantChangesOnly: false,
  logLevel: BackgroundGeolocation.LOG_LEVEL_OFF,
}

Code

BackgroundGeolocation.watchPosition(
  location => {
    console.log(location);
  },
  errorCode => {
    console.error('watchPosition', errorCode);
  },
  {
    interval: 1000,
    desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
  },
);

Crysp avatar Oct 17 '23 14:10 Crysp

Testing in the simulator is the most accurate way to test timing since it isn't affected by the environment. The timing of interval (using stopwatch) is correct and exact. There is no guarantee that watchPosition on an actual device will return a location at the exact interval, particularly while indoors, where GPS doesn't work.

  • interval: 1000

IMG_1343

  • interval: 5000

IMG_1341

  • interval: 10000

IMG_1342

christocracy avatar Oct 17 '23 17:10 christocracy

I understand that intervals could be various, but difference in 10 seconds is too much when interval is 1 second. iPhone simulator has the same behaviour and it doesn't few cases, it works like if interval was set 10 seconds instead of 1 second

Crysp avatar Oct 17 '23 18:10 Crysp

Did you see my stop-watch images above? They are perfect.

christocracy avatar Oct 17 '23 18:10 christocracy

Yes. Looks like side effects produce this bug. Because I run watchPosition in clear environment and it works perfectly

Crysp avatar Oct 17 '23 19:10 Crysp

What do you mean by "side-effects"?

I don't agree this is a bug.

What are you using .watchPosition for?

christocracy avatar Oct 17 '23 19:10 christocracy

"side effects" is my code.

I'm using it for foreground location updates to display user on map.

Crysp avatar Oct 18 '23 07:10 Crysp

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar May 06 '24 01:05 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar May 20 '24 01:05 github-actions[bot]