Search code examples
androidandroid-workmanager

Android - How to limit number of specific Worker instances running in parallel in WorkManager?


I was wondering is there any way to limit number of parallel workers of only one type.

So I have UploadWorker, witch backups single file. I want to be able to limit how much uploads go in parallel.

And with

    val configuration = Configuration.Builder()
    .setExecutor(Executors.newFixedThreadPool(2))
    .build()
WorkManager.initialize(context, configuration)

it is all good for upload part. I can just dump work requests as I collect new items for upload.

But I have other Workers, and I do not want for UploadWorker work requests to finish, but I want those to run as soon as possible.

How can I manage that?

Thanks


Solution

  • I have figured it out.

    If I use regular worker for UploadWorker, and CoroutineWorker for others, then UploadWorker will run on configured Executor, while CoroutineWorker runs on default Dispatcher or any other that I want.