Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WorkManager: multiple workers OR one long running worker

I have a task to upload photos (can be one photo or can be 50 or 100 photos) to the cloud and once uploaded photos are done display a notification. The API supports one photo at a time and I need to update the UI with the uploaded photo if it was successful or not.

Obviously what comes to my mind is to create a service but since Android now suggests using WorkManager to do all our foreground/background work, I started investigating Workers and WorkManager.

I would like to hear the community's opinion regarding the following:

Knowing the requirements mentioned above, would you rather go with

  1. Create a single long running worker that does all the uploads sequentially and then create a notification once they are done, or

  2. Have one worker that all it does is upload a single photo and create for every image upload a OneTimeWorkRequest and the notification as a separate worker.

    Example:

workManager
        .beginUniqueWork(workUniqueId, ExistingWorkPolicy.REPLACE, uniqueUploadPhotoWorkRequestList)
        .then(notificationWorkRequest)
        .enqueue()
like image 757
Karim Fikani Avatar asked Jan 22 '26 11:01

Karim Fikani


1 Answers

You need to ask youself.

What is the work I am doing? What are the input parameters? What are the output parameters?

Is it is something like:

  • I want to upload images.
  • My input is some directory
  • My output is a notification on end

Sounds more like a single work. Why to bother with so much overhead.

But at the same time consider what will happen if the work is interrupted. Then you have the situation with: "half a work" so you need to manage this on your own. With a queue of images you can just start the upload from the start which is much simpler.

But this also depends on the server on the other side. How it understands uploads on chunks. How much time it keeps the chunks for resuming.

You should write yourself stuff like this. Prost and cons. A lot of data and when you see you will have a your answer. If you want you can buy a rubber duck. But at moment your question looks like more of a "thought" you are sharing with us.

like image 190
Yavor Mitev Avatar answered Jan 24 '26 03:01

Yavor Mitev