I’m working with Android’s WorkManager and have two workers: WorkerA and WorkerB. WorkerA enqueues WorkerB from within its doWork() method.
Here’s the scenario I’m facing:
When WorkerA is enqueued for the first time, WorkerB executes normally. It finishes, returns a result, and WorkerA processes it, eventually finishing as expected.
When WorkerA is enqueued a second time, the state of WorkerB shows as “running,” but I don’t see any logs from WorkerB. It’s as though the worker is stuck or not executing its code.
However, when I close the app and restart it, WorkerB’s code executes, and logs appear.
What I’ve tried:
I ensured that WorkerB was properly enqueued in WorkerA using WorkManager.getInstance().enqueue().
Checked the logs for any errors but found none.
Questions:
Why might WorkerB not execute properly when WorkerA enqueues it the second time?
Why does restarting the app cause WorkerB to execute its code?
WorkManager setup:
Code of enqueuing WorkerA
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(UCEMSCommissioningWorker.class)
.addTag("ucemsWorker")
.setInitialDelay(10000, TimeUnit.SECONDS)
.build();
workManager.enqueueUniqueWork(UCEMSCommissioningWorker.class.getName(), ExistingWorkPolicy.KEEP, workRequest);
Log.i("Recommission", "BLE Service UCEMS: startWorker: worker enqueued");
workManager.getWorkInfoByIdLiveData(workRequest.getId()).observeForever(observer);
WorkerA enqueues WorkerB using WorkManager.enqueue().
val data = Data.Builder().putString(
"TAG_FROM_ACTIVITY",
"UCEMS",
).build()
val request = OneTimeWorkRequest.Builder(DeviceScanWorker::class.java)
.setInputData(data)
.build()
workManager.enqueueUniqueWork("DeviceScanWorker", ExistingWorkPolicy.REPLACE, request)
Both workers are OneTimeWorkRequests.
WorkerA and WorkerB aren’t using UniqueWork.
Any insights into why this is happening and how I can fix it would be greatly appreciated!