本文介绍了如何返回ListenableFuture< Result>与工作管理器2.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎ListenableWorker不再具有@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)限制,但是我找不到或找不到有关如何正确返回ListenableFuture的示例.结果>在我覆盖的startWork()函数中.

It seems ListenableWorker no longer has the @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) restriction however I can't figure out or find examples on how to properly return a ListenableFuture< Result> in my overriden startWork() function.

据我所知,唯一的选择是返回SettableFuture.create< Result>(),但这仍然需要禁止显示"RestrictedApi"警告/错误.

As far as I can tell, the only option is to return a SettableFuture.create< Result>() but that still requires suppressing the "RestrictedApi" warning/error.

有人知道更简单的方法吗?

Anyone know a simpler way ?

据我所知,这是使用CallbackToFutureAdapter.Completer

This is the way to do it as far as I've gathered which makes use of CallbackToFutureAdapter.Completer

override fun startWork(): ListenableFuture<Result> {
    return CallbackToFutureAdapter.getFuture({
        it.set(ListenableWorker.Result.success())
    })

}

推荐答案

按照 ListenableWorker文档页面中的线程:

  1. 如果您使用番石榴,请使用ListeningExecutorService.
  2. 否则,请在gradle文件中包含 councurrent-futures 使用 CallbackToFutureAdapter .
  1. If you use Guava, use ListeningExecutorService.
  2. Otherwise, include councurrent-futures in your gradle file and use CallbackToFutureAdapter.

这篇关于如何返回ListenableFuture&lt; Result&gt;与工作管理器2.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:13