I have a moderately long-running (a minute or two) background process, which I'm running as a Worker using WorkManager. There are two places from where it can be started - either from the UI, or from another background process.
In the case of the UI, of course I can observe it as LiveData, and update when it changes.
However in the case of the background process, I'd like to be able to enqueue the Worker, and then wait for it to complete, blocking until it's finished.
I've tried using runBlocking, for example:
val request = OneTimeWorkRequestBuilder<LibraryCacheWorker>()
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build()
runBlocking { WorkManager.getInstance(context).enqueue(request).result.await() }
But it just returns straight away while the process continues on another thread. How can I wait for the actual work to finish before continuing?
I resolved this using a CountDownLatch in the end.
The background process calls the function synchronously, as per CommonsWare's suggestion, and the UI starts a Worker, which calls the function. However the function itself uses a CountDownLatch to detect whether it is already running in another thread.
Like this: