本文介绍了如何确保WorkManager取消我的工人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WorkManager文档中提到取消工作人员是尽力而为

It's mentioned in WorkManager documentation that cancelling a Worker is best-effort

如果我有一个使用案例,要求Worker在调用取消方法之一时被取消是强制性的,怎么办?

What if i have a use case that it's mandatory that the Worker gets cancelled upon calling one of the cancelling methods?

推荐答案

撰写本文时,WorkManager只能尽最大努力取消工作.特别是,如果计划将某个任务运行,并且您将其取消,WorkManager会将其从计划中删除.

As you wrote WorkManager can only try with a best-effort to cancel a work. In particular, if a task is scheduled to run, and you cancel it, WorkManager will remove it from the schedule.

但是,如果任务已经在运行,则WorkManager无法安全地中断它.最好的选择是您编写Worker类时要注意外部取消. 使用WorkManager 讨论(大约15分钟)中对此进行了介绍 a>记录在2018年的Android开发者峰会上.

However, if the tasks it's already running, WorkManager cannot safely interrupt it. The best option is that you write your Worker class taking care of an external cancellation. This has been covered in the Working with WorkManager talk (around minute 15) recorded at the Android Developer Summit 2018.

要成为好公民,您可以使用以下方法合并WorkManager: ListenableWorker.isStopped() .您可以将其与 onStopped 回调结合进行清理您或操作系统请求停止任务时输入的代码.

To be a good citizen you can pool WorkManager using the method: ListenableWorker.isStopped(). You can combine this with the onStopped callback to cleanup your code when you or the OS request to stop the task.

这篇关于如何确保WorkManager取消我的工人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:12