-->

can I always use WorkManager instead of coroutines

2019-05-10 06:20发布

问题:

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 ?

回答1:

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.



回答2:

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.



回答3:

Has others replied, WorkManager solves a different problem than Kotlin's corountines or a reactive library like RxJava.

WorkManager is now available as beta and additional documentation is produced that hopefully makes this clear. One of these documents is the blog post I worte with some colleagues: Introducing WorkManager, where you can read:

A common confusion about WorkManager is that it’s for tasks that needs to be run in a “background” thread but don’t need to survive process death. This is not the case. There are other solutions for this use case like Kotlin’s coroutines, ThreadPools, or libraries like RxJava. You can find more information about this use case in the guide to background processing.



回答4:

When to use WorkManager The WorkManager library is a good choice for tasks that are useful to complete, even if the user navigates away from the particular screen or your app.

Some examples of tasks that are a good use of WorkManager:

Uploading logs

Applying filters to images and saving the image

Periodically syncing local data with the network

WorkManager offers guaranteed execution, and not all tasks require that. As such, it is not a catch-all for running every task off of the main thread. For more details about when to use WorkManager, check out the Guide to background processing.

WorkManager is part of Android Jetpack and an Architecture Component for background work that needs a combination of opportunistic and guaranteed execution. Opportunistic execution means that WorkManager will do your background work as soon as it can. Guaranteed execution means that WorkManager will take care of the logic to start your work under a variety of situations, even if you navigate away from your app.

WorkManager is a simple, but incredibly flexible library that has many additional benefits. These include:

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