can I always use WorkManager instead of coroutines?

后端 未结 5 1642
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 01:53

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 di

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-09 02:16

    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.

提交回复
热议问题