flutter_workmanager icon indicating copy to clipboard operation
flutter_workmanager copied to clipboard

Issue with WorkManager Periodic Task Scheduling

Open shubham0809200 opened this issue 1 year ago • 6 comments
trafficstars

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

shubham0809200 avatar Feb 19 '24 05:02 shubham0809200

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

prabhakar1992 avatar Feb 26 '24 09:02 prabhakar1992

I have the same issue. Did you solve @prabhakar1992 @shubham0809200 ?

sinhpn92 avatar Jul 29 '24 11:07 sinhpn92

I have not been able to find a solution for the problem.

shubham0809200 avatar Jul 30 '24 05:07 shubham0809200

I have not been able to find a solution for the problem.

I checked by running the command e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"workmanager.background.task"]. However, both the predict and progress tasks only run for about 30 seconds and then stop. They are not running as true long-running tasks. When I tested it in practice, by building a profile for my phone, I noticed that when I plugged in the charger and wasn't using it, the progress task started. But it stopped after running for just 15 seconds. I am not sure if this is an issue with iOS. Do you use any alternative libraries or solutions for this?

sinhpn92 avatar Jul 30 '24 07:07 sinhpn92

I would suggest that you implement a non-removable notification to make sure the user knows that the app is working on a background task.

I guess as the task is running in the background, which might be causing Android/iOS to terminate the process for security reasons. if you give a notification it will ensure that the user is aware of the process

shubham0809200 avatar Jul 30 '24 07:07 shubham0809200