Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PeriodicWorkRequestBuilder how to avoid double scheduling?

I'm using the new WorkManager, and was wondering, when I schedule a Periodic work, how can I avoid re-schedule it, is it automatically handled by OS?

like image 458
Chen Kinnrot Avatar asked Feb 25 '26 17:02

Chen Kinnrot


1 Answers

If you need to check already running work manager just because you don't want duplicate works. You can simply use enqueueUniquePeriodicWork()

This method allows you to enqueue a uniquely-named PeriodicWorkRequest, where only one PeriodicWorkRequest of a particular name can be active at a time. For example, you may only want one sync operation to be active. If there is one pending, you can choose to let it run or replace it with your new work.

So you don't need to worry about duplicacy about works.

 workmanager.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.KEEP , photoCheckWork);
  • Where TAG is unique name by which work manager will check duplicacy.
  • You can choose between ExistingPeriodicWorkPolicy.KEEP and ExistingPeriodicWorkPolicy.REPLACE.
like image 58
Pratik Butani Avatar answered Feb 27 '26 05:02

Pratik Butani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!