Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I always use WorkManager instead of coroutines?

I wonder why should I bother with rx or coroutines when there is brilliant solution as WorkManager. But for almost all tutorials they use coroutines so may be WorkManager has disadvantages ?

like image 605
Nurseyit Tursunkulov Avatar asked Nov 25 '18 02:11

Nurseyit Tursunkulov


5 Answers

From official Documentation:

While coroutines are the recommended solution for certain use cases, you should not use them for persistent work. It is important to note that coroutines is a concurrency framework, whereas WorkManager is a library for persistent work.

like image 58
Muhammed naseef Koppilakkal Avatar answered Nov 13 '22 06:11

Muhammed naseef Koppilakkal


The scope of both is different. WorkManager is to schedule deferrable (for any later time) or immediately. tasks asynchronously.

As documentation says

The WorkManager API makes it easy to specify deferrable, asynchronous tasks and when they should run. These APIs let you create a task and hand it off to WorkManager to run immediately or at an appropriate time.

On the other hand, coroutines are designed to compute a given task only immediately and asynchronously.

Also Internally, coroutines and WorkManager work differently. Work manager heavily depends on Android system components like Services, Alarm manager, etc to schedule the work whereas coroutines schedule the work on Worker Threads and also is a language feature unlike WorkManager (API). So it is safe to say that coroutines do not go beyond your application. On the other hand, WorkManager can even execute the given tasks when your application is not active. for instance, background services.

Also as Marko answered, using coroutines will lead to better code readability and quality due to their fundamental design. I would also like to include ANKO, Its a great library that provides a helpful API around coroutines for Android.

like image 27
shubham vashisht Avatar answered Nov 13 '22 06:11

shubham vashisht


Background tasks fall into one of the following main categories:

  1. Immediate
  2. Deferred
  3. Exact

To categorize a task, answer the following questions:

Does the task need to complete while the user is interacting with the application?

If so, this task should be categorized for immediate execution. If not, proceed to the second question.

Does the task need to run at an exact time?

If you do need to run a task at an exact time, categorize the task as exact.

Most tasks don't need to be run at an exact time. Tasks generally allow for slight variations in when they run that are based on conditions such as network availability and remaining battery. Tasks that don't need to be run at an exact time should be categorized as deferred.

Use Kotlin Coroutine when a task needs to execute immediately and if the task will end when the user leaves a certain scope or finishes an interaction.

Use WorkManager when a task needs to execute immediately and need continued processing, even if the user puts the application in the background or the device restarts

Use AlarmManager when a task that needs to be executed at an exact point in time

For more details, visit this link

like image 28
Mehedi Hasan Avatar answered Nov 13 '22 06:11

Mehedi Hasan


If your goal is writing clean code without explicitly constructed callbacks you pass to background tasks, then you'll find that coroutines are the only option.

Using coroutines by no means precludes using WorkManager or any other tool for background operations of your choosing. You can adapt the coroutines to any API that provides callbacks as a means to continue the execution with the results of background operations.

like image 28
Marko Topolnik Avatar answered Nov 13 '22 04:11

Marko Topolnik


WorkManager:

Support for both asynchronous one-off and periodic tasks Support for constraints such as network conditions, storage space, and charging status Chaining of complex work requests, including running work in parallel Output from one work request used as input for the next Handles API level compatibility back to API level 14(see note) Works with or without Google Play services Follows system health best practices LiveData support to easily display work request state in UI Waits proper time to run.

Coroutines:

Clean code, works under the hood in a different way. Run immediately.

So depending on your requirements choose the better option.

like image 20
Nurseyit Tursunkulov Avatar answered Nov 13 '22 06:11

Nurseyit Tursunkulov