显示运行WorkManager Worker的详细进度 [英] Showing detailed progress for running WorkManager workers

查看:443
本文介绍了显示运行WorkManager Worker的详细进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用新的JetPack WorkManager (链接到 codelabs )组件(在应用的沙箱分支).我现有的系统运行良好,但是WorkManager中的某些新功能会派上用场(例如,链接).

I want to replace the job scheduling aspect of my existing data syncing system with the new JetPack WorkManager (link to codelabs) component (in a sandbox branch of the app). My existing system works well but some of the new features in WorkManager would come in handy (e.g. chaining).

我当前的系统使用共享的LiveData来将正在进行的作业的进度传达给在其上观察到的任何UI元素(在我的情况下为RecyclerView)(实际上,我将ViewModel中的SwitchMapping放入列表中)的SyncItem s)

My current system uses a shared LiveData to communicate the progress from a job in progress to any UI element (RecyclerView in my case) observing on it (I'm actually SwitchMapping in the ViewModel into a list of SyncItems)

data class SyncItem(
        val title: String,
        private var _progress: Int,
        var total: Int) : BaseObservable() {

    var progress: Int
        @Bindable get() = _progress
        set(value) {
            _progress = value
            notifyPropertyChanged(BR.progress)
        }
}

新的WorkManager组件具有几种方法(getStatusByIdgetStatusesByTag等),可用于检索具有一个或多个WorkStatus es的LiveData,但这些方法仅报告课程状态(正在运行,成功,失败,已取消).

The new WorkManager component has several methods (getStatusById, getStatusesByTag, etc.) that can be used to retrieve a LiveData with one or more WorkStatuses, but these only report a course-grained status (running, success, failed, cancelled).

建议将进度(例如已下载的546/1234个项目")传达到用户界面的建议方式是什么?与用户界面相比,setOutputData/getOutputData对似乎在Worker之间(在链接时我需要)之间进行通信的使用更多.

What is the recommended way of communicating progress (e.g. '546/1234 items downloaded') to the UI? The setOutputData/getOutputData pair seems to be used more to communicate between Workers (which I need when chaining) than with the UI.

附件是用户打开同步状态页面(已完成2项,正在进行中)时(在使用我的旧方法的应用的[测试]版本中)的外观的屏幕截图.

Attached is a screenshot of what it looks like (in a [test] version of my app using my old method) when a user opens the sync status page (2 items completed, rest in progress).

在最终产品中,用户将能够取消任何进行中的工作并重新发出一次性工作请求.通常,作业将由PeriodicWorkRequest解雇.

In the final product the user will be able to cancel any jobs in progress and re-issue once-off work requests. Normally the jobs will be fired off by PeriodicWorkRequest.

推荐答案

做到这一点的最佳方法是将中间进度写入您自己的数据存储并公开LiveData<>.我们正在考虑在将来的Alpha中添加此功能.

The best way to do it is to write intermediate progress to your own data store and expose a LiveData<>. We are considering adding this feature in a future alpha.

这篇关于显示运行WorkManager Worker的详细进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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