fluttercommunity / flutter_workmanager

A Flutter plugin which allows you to execute code in the background on Android and iOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with WorkManager Periodic Task Scheduling

shubham0809200 opened this issue · comments

Issue with WorkManager Periodic Task Scheduling

Description

I encountered an issue with WorkManager while scheduling a periodic task with a 2-hour interval and a constraint that the network should be available. Here's the scenario:

  1. The periodic task was initially scheduled for execution at 1:00 AM. which went well
  2. I turned off the network connection until 4:00 AM, causing the task to miss its scheduled execution at 3:00 AM
  3. Due to the unavailability of the network, the task was executed later than expected, at 4:00 AM.
  4. Subsequently, instead of maintaining the 2-hour interval from the last executed task, the next execution occurred at 6:00 AM.

Expected Behavior

The periodic task should maintain its scheduled interval of 2 hours, irrespective of delays caused by network unavailability. In the scenario described above, the task should have been executed at 3:00 AM and then at 5:00 AM, as per the scheduled interval.

I understand the scenario of why it wasn't able to run at 3:00 AM but it should have gone off at 5:00 AM as per its schedule.

I have provided my code for reference the the end of this issue.

Note: If there are any available solutions or recommendations to address this scenario, I would greatly appreciate any assistance or guidance provided. Thank you.

CODE

Workmanager().registerPeriodicTask(
  "backupData",
  "backupData",
  frequency: Duration(hours: 2),
  initialDelay: timeDuration,
  constraints: Constraints(
    networkType: NetworkType.connected,
  ),
  backoffPolicy: BackoffPolicy.linear,
  existingWorkPolicy: ExistingWorkPolicy.replace,
  outOfQuotaPolicy: OutOfQuotaPolicy.run_as_non_expedited_work_request,
  backoffPolicyDelay: Duration(seconds: 10),
);

@ened I have also got an issue in workmanager, periodic task not triggered when app is in killed state.