Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WorkManager - remove periodic worker for good

I had an update with the following code that adds a new periodic worker every three hours.

fun runCouponValidatorWorker() {
    val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
    val worker = PeriodicWorkRequestBuilder<CouponValidatorWorker>(3, TimeUnit.HOURS).setConstraints(constraints).build()
    WorkManager.getInstance()?.enqueueUniquePeriodicWork("couponValidatorWorker", ExistingPeriodicWorkPolicy.REPLACE, worker)
}

I'd like to release an update that will "kill" this worker and every scheduled instance of this worker.

What would be the best way to do that?

like image 302
Rom Shiri Avatar asked Jul 22 '18 12:07

Rom Shiri


2 Answers

Did you try simply calling this:

WorkManager.getInstance(context).cancelUniqueWork("couponValidatorWorker")
like image 85
Arseny Levin Avatar answered Sep 28 '22 09:09

Arseny Levin


WorkManager.getInstance().cancelAllWorkByTag(TAG)
like image 38
suyash saurabh Avatar answered Sep 28 '22 09:09

suyash saurabh