transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.62k stars 428 forks source link

Updating schedule with setConfig does not stop the previous schedule. #1507

Closed emmanuelfrancis closed 3 months ago

emmanuelfrancis commented 2 years ago

Your Environment

// On a button "Start Scheduler" click sets the schedule: BackgroundGeolocation.setConfig({ "schedule" : [ "2 20:00-21:00 geofence", "3 20:00-21:30 geofence", ], scheduleUseAlarmManager: true, }) .then((state1) => { BackgroundGeolocation.startSchedule().then((state2) => { }); }) .catch((error) => { });

// On a button "Remove Scheduler" click sets the schedule:

BackgroundGeolocation.setConfig({ "schedule" : [ "3 20:00-21:30 geofence", ], scheduleUseAlarmManager: true, }) .then((state1) => { BackgroundGeolocation.startSchedule().then((state2) => { }); }) .catch((error) => { });


## Expected Behavior
<!--- On click of "Remove Scheduler", the scheduler should stop. -->

## Actual Behavior
<!--- On click of "Remove scheduler, the scheduler starts at 8:00 PM and fires onSchedule-->

## Steps to Reproduce
<!--- reproduce this issue; include code to reproduce, if relevant -->
1. Installed the app on Monday before 8:00 PM
2. Click on the button "Start Scheduler" 
3. Click on the button "Remove Scheduler"
4. At 8:00 onSchedule will be called.

## Context
<!--- What were you trying to do? -->

## Debug logs
<!-- include iOS / Android logs
- ios XCode logs,
- use #getLog #emailLog methods (@see docs)
- Android: $ adb logcat -s TSLocationManager
-->
<details><summary>Logs</summary>

``` <!-- Syntax highlighting:  DO NOT REMOVE -->
PASTE_YOUR_LOGS_HERE

christocracy commented 2 years ago

Updating schedule with setConfig does not stop the previous schedule

Why not?

emmanuelfrancis commented 2 years ago

@christocracy I am trying to stop the schedule, but if I remove the schedule from the schedule array and call setConfig by passing new array, it still fires onSchedule on previously set time. I also tried calling BackgroundGeolocation.stopSchedule before setting new schedule.

christocracy commented 2 years ago

What happens if you await BackgroundGeolocation.stopSchedule() before .setConfig(newSchedule)?

emmanuelfrancis commented 2 years ago

Does not work, I wrote exact same, also tried calling .stop await BackgroundGeolocation.stopSchedule((state) => { }); await BackgroundGeolocation.stop((state) => { }); BackgroundGeolocation.setConfig({ schedule: newSchedule, scheduleUseAlarmManager: true, })

christocracy commented 2 years ago

Here I let a schedule event fire and observe the next item get scheduled

06-27 11:37:00.036 31092 31092 I TSLocationManager: ╔═════════════════════════════════════════════
06-27 11:37:00.036 31092 31092 I TSLocationManager: ║ 📅  Schedule alarm fired!  enabled: false, trackingMode: 0
06-27 11:37:00.036 31092 31092 I TSLocationManager: ╠═════════════════════════════════════════════
06-27 11:37:00.045 31092 31092 D TSLocationManager:   📅  Day #2: Searching schedule for alarms...
06-27 11:37:00.046 31092 31092 I TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-27 11:37:00.046 31092 31092 I TSLocationManager:   📅  Scheduled Alarm: ON at 11:38 on Monday
06-27 11:37:00.046 31092 31092 D TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-27 11:37:00.046 31092 31092 D TSLocationManager:   ℹ️  Schedule with AlarmManager

Then I call .stopSchedule():

06-27 11:37:03.026 31092 31092 I TSLocationManager: [c.t.l.s.TSScheduleManager stop]
06-27 11:37:03.026 31092 31092 I TSLocationManager:   🔴  Scheduler OFF

I see no more events fire. The scheduler is stopped. The last scheduled event 📅 Scheduled Alarm: ON at 11:38 on Monday does not fire.

christocracy commented 2 years ago

Google PIxel 6 @ 12.

christocracy commented 2 years ago

My logs above prove that calling .stopSchedule() cancels a currently scheduled event.

christocracy commented 2 years ago

You can test the scheduler by scheduling events to occur a minute into the future. You don’t have to wait hours.

emmanuelfrancis commented 2 years ago

Calling .stopSchedule() stops all future/upcoming schedules. Below are my findings:

// Not working
await BackgroundGeolocation.stopSchedule()
BackgroundGeolocation.setConfig({ schedule: newSchedule, scheduleUseAlarmManager: true, }) .then((state1) => {
      BackgroundGeolocation.startSchedule().then((state2) => {
 });
})

Above code does not stop previous schedule. It keeps previous schedule as well as add new schedule.

I tried below and it works:

// This works
await BackgroundGeolocation.stopSchedule()
await BackgroundGeolocation.setConfig({
    schedule: [],
  }).then((stateClear) => {
    console.log('clean schedule before updates ', stateClear);
  });
BackgroundGeolocation.setConfig({ schedule: newSchedule, scheduleUseAlarmManager: true, }) .then((state1) => {
      BackgroundGeolocation.startSchedule().then((state2) => {
 });
})
christocracy commented 2 years ago

Thanks, I’ll test that.

christocracy commented 2 years ago

I'm not seeing any issue with changing the schedule on-the-fly. I do not get any incorrect schedule events occur. It does not seem to be required to .stopSchedule() to update the schedule.

await BackgroundGeolocation.setConfig((
      schedule: [
        '1-7 13:00-18:00'
      ]
});
await BackgroundGeolocation.startSchedule();

await BackgroundGeolocation.setConfig({
      schedule: [
        '1-7 12:55-18:00'
      ]
});
06-28 12:52:36.757  5841  5841 D TSLocationManager: [c.t.l.adapter.TSConfig d] ℹ️   Persist config, dirty: [schedule]
06-28 12:52:36.760  5841  5841 D TSLocationManager: [c.t.l.s.TSScheduleManager restart]
06-28 12:52:36.760  5841  5841 D TSLocationManager:   📅  Schedule changed:  restarting...
06-28 12:52:36.763  5841  5841 I TSLocationManager: [c.t.l.s.TSScheduleManager start]
06-28 12:52:36.763  5841  5841 I TSLocationManager: ╔═════════════════════════════════════════════
06-28 12:52:36.763  5841  5841 I TSLocationManager: ║ 🎾  Scheduler ON
06-28 12:52:36.763  5841  5841 I TSLocationManager: ╠═════════════════════════════════════════════
06-28 12:52:36.763  5841  5841 I TSLocationManager: ╟─ Schedule[13:00-18:00, Days: [1, 2, 3, 4, 5, 6, 7], trackingMode: 1]
06-28 12:52:36.763  5841  5841 I TSLocationManager: ╚═════════════════════════════════════════════
06-28 12:52:36.764  5841  5841 D TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-28 12:52:36.764  5841  5841 D TSLocationManager:   📅  Day #3: Searching schedule for alarms...
06-28 12:52:36.765  5841  5841 I TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-28 12:52:36.765  5841  5841 I TSLocationManager:   📅  Scheduled Alarm: ON at 13:00 on Tuesday
06-28 12:52:36.765  5841  5841 D TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-28 12:52:36.765  5841  5841 D TSLocationManager:   ℹ️  Schedule with AlarmManager
06-28 12:52:36.772  5841  5841 W TSLocationManager: [c.t.l.s.TSScheduleManager start] Scheduler already started.  IGNORED
06-28 12:52:36.796  5841  5841 D TSLocationManager: [c.t.l.adapter.TSConfig d] ℹ️   Persist config, dirty: [schedule]
06-28 12:52:36.798  5841  5841 D TSLocationManager: [c.t.l.s.TSScheduleManager restart]
06-28 12:52:36.798  5841  5841 D TSLocationManager:   📅  Schedule changed:  restarting...
06-28 12:52:36.804  5841  5841 I TSLocationManager: [c.t.l.s.TSScheduleManager start]
06-28 12:52:36.804  5841  5841 I TSLocationManager: ╔═════════════════════════════════════════════
06-28 12:52:36.804  5841  5841 I TSLocationManager: ║ 🎾  Scheduler ON
06-28 12:52:36.804  5841  5841 I TSLocationManager: ╠═════════════════════════════════════════════
06-28 12:52:36.804  5841  5841 I TSLocationManager: ╟─ Schedule[12:55-18:00, Days: [1, 2, 3, 4, 5, 6, 7], trackingMode: 1]
06-28 12:52:36.804  5841  5841 I TSLocationManager: ╚═════════════════════════════════════════════
06-28 12:52:36.805  5841  5841 D TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-28 12:52:36.805  5841  5841 D TSLocationManager:   📅  Day #3: Searching schedule for alarms...
06-28 12:52:36.805  5841  5841 I TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-28 12:52:36.805  5841  5841 I TSLocationManager:   📅  Scheduled Alarm: ON at 12:55 on Tuesday
06-28 12:52:36.806  5841  5841 D TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-28 12:52:36.806  5841  5841 D TSLocationManager:   ℹ️  Schedule with AlarmManager
.
.
.
06-28 12:55:00.042  5841  5841 I TSLocationManager: [c.t.l.scheduler.ScheduleEvent onScheduleEvent]
06-28 12:55:00.042  5841  5841 I TSLocationManager: ╔═════════════════════════════════════════════
06-28 12:55:00.042  5841  5841 I TSLocationManager: ║ 📅  Schedule alarm fired!  enabled: true, trackingMode: 1
06-28 12:55:00.042  5841  5841 I TSLocationManager: ╠═════════════════════════════════════════════
06-28 12:55:00.069  5841  5841 D TSLocationManager: [c.t.locationmanager.util.c h]
06-28 12:55:00.069  5841  5841 D TSLocationManager:   ℹ️  LocationAuthorization: Permission granted
06-28 12:55:00.069  5841  5841 I TSLocationManager: [c.t.l.service.TrackingService changePace]
06-28 12:55:00.069  5841  5841 I TSLocationManager:   🔵  setPace: false → false
06-28 12:55:00.071  5841  5841 D TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-28 12:55:00.071  5841  5841 D TSLocationManager:   📅  Day #3: Searching schedule for alarms...
06-28 12:55:00.072  5841  5841 I TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-28 12:55:00.072  5841  5841 I TSLocationManager:   📅  Scheduled Alarm: OFF at 18:00 on Tuesday
06-28 12:55:00.072  5841  5841 D TSLocationManager: [c.t.l.s.TSScheduleManager a]
06-28 12:55:00.072  5841  5841 D TSLocationManager:   ℹ️  Schedule with AlarmManager

I have not seen you post any logs yet? Are you observing your logs?

christocracy commented 2 years ago

When .setConfig() is called with a new schedule, the plugin correctly detects that and "resets" itself.

06-28 12:52:36.796  5841  5841 D TSLocationManager: [c.t.l.adapter.TSConfig d] ℹ️   Persist config, dirty: [schedule]
06-28 12:52:36.798  5841  5841 D TSLocationManager: [c.t.l.s.TSScheduleManager restart]
06-28 12:52:36.798  5841  5841 D TSLocationManager:   📅  Schedule changed:  restarting...
emmanuelfrancis commented 2 years ago

Please check the attached logs.

  1. at 06-29 14:15:02.825 31805 31939 I ReactNativeJS: '4 14:18-14:20 geofence', -- a schedule exists for 14:18
  2. at 06-29 14:15:24.360 31805 31939 I ReactNativeJS: schedule: -- schedule was removed.
  3. but at 06-29 14:18:00.302 31805 31939 I ReactNativeJS: 'geofences list onSchedule ', 58 -- onSchedule was fired.

I tried fetching logs using getLogs but that failed with error "Failed to fetch logs", so its from adb I also do not see any logs which say "Schedule changed" after calling setConfig. TSLocationManager logs appeared only when app was started and when onSchedule and onGeofence fired. When I called setConfig the promise was resolved properly and you can see the logs with "- BackgroundGeolocation setConfig setScheduleUpdate: with state object. geologs.log

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

github-actions[bot] commented 3 months ago

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