可以始终使用WorkManager代替协程吗? [英] can I always use WorkManager instead of coroutines?

查看:309
本文介绍了可以始终使用WorkManager代替协程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当有出色的解决方案作为WorkManager时,为什么要理会rx或协程.但是对于几乎所有教程,他们都使用协程,所以WorkManager可能有缺点吗?

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 ?

推荐答案

两者的范围是不同的. WorkManager将安排可延期(任何以后的时间)或立即进行. 异步执行任务.

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

如文档所述

As documentation says

通过WorkManager API,可以轻松指定可延迟,异步的 任务以及何时运行.这些API可让您创建任务并 将其交给WorkManager立即运行或在适当的时候运行 时间.

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.

也 在内部,协程和WorkManager的工作方式不同.工作管理器在很大程度上依靠Android系统组件(例如服务,警报管理器等)来安排工作,而协程则在工作线程上安排工作,并且它是一种与WorkManager(API)不同的语言功能.因此可以肯定地说,协程不会超出您的应用范围.另一方面,当您的应用程序处于非活动状态时,WorkManager甚至可以执行给定的任务.例如后台服务.

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.

正如Marko回答的那样,由于协程的基本设计,使用协程将导致更好的代码可读性和质量. 我还想加入 ANKO ,它是一个很棒的库,为Android协程提供了有用的API.

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.

这篇关于可以始终使用WorkManager代替协程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆