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

Updating schedule with setConfig does not stop the previous schedule.

Open emmanuelfrancis opened this issue 3 years ago • 14 comments

Your Environment

  • Plugin version: 4.7.1
  • Platform: Android
  • OS version: Android 12
  • Device manufacturer / model: Samsung Galaxy M21
  • React Native version (react-native -v): 0.66.3
  • Plugin config
BackgroundGeolocation.ready({
      desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_MEDIUM,
      stopOnTerminate: false,
      startOnBoot: true,
      showsBackgroundLocationIndicator: true,
      locationAuthorizationRequest: 'Always',
      disableMotionActivityUpdates: false,
      activityType: BackgroundGeolocation.ACTIVITY_TYPE_OTHER,
      geofenceModeHighAccuracy: true,
      scheduleUseAlarmManager: true,
      logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
      enableHeadless: true,
      disableLocationAuthorizationAlert: true,
      debug: __DEV__,      
    }).then((state) => {  
      console.log("ready state ", state)
    }).catch(error => {
      console.log("- BackgroundGeolocation error: ", error);
    });
  }  

// 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

Actual Behavior

Steps to Reproduce

  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

Debug logs

Logs
PASTE_YOUR_LOGS_HERE

emmanuelfrancis avatar Jun 27 '22 14:06 emmanuelfrancis

Updating schedule with setConfig does not stop the previous schedule

Why not?

christocracy avatar Jun 27 '22 14:06 christocracy

@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.

emmanuelfrancis avatar Jun 27 '22 15:06 emmanuelfrancis

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

christocracy avatar Jun 27 '22 15:06 christocracy

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, })

emmanuelfrancis avatar Jun 27 '22 15:06 emmanuelfrancis

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 avatar Jun 27 '22 15:06 christocracy

Google PIxel 6 @ 12.

christocracy avatar Jun 27 '22 15:06 christocracy

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

christocracy avatar Jun 27 '22 15:06 christocracy

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

christocracy avatar Jun 27 '22 16:06 christocracy

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) => {
 });
})

emmanuelfrancis avatar Jun 28 '22 06:06 emmanuelfrancis

Thanks, I’ll test that.

christocracy avatar Jun 28 '22 09:06 christocracy

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 avatar Jun 28 '22 16:06 christocracy

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...

christocracy avatar Jun 28 '22 17:06 christocracy

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

emmanuelfrancis avatar Jun 29 '22 09:06 emmanuelfrancis

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.

stale[bot] avatar Sep 21 '22 03:09 stale[bot]