WorkManager:ResolvableFuture只能在相同的库组前缀中调用 [英] WorkManager: ResolvableFuture can only be called from within the same library group prefix

查看:71
本文介绍了WorkManager:ResolvableFuture只能在相同的库组前缀中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,在我的 ListenableWorker 类中,我使用了以下内容:

For some time in my ListenableWorker class I have used the following:

public ListenableFuture<Result> startWork() {
    ResolvableFuture<Result> resolvableFuture = ResolvableFuture.create();
    startSomeAsyncStuff(resolvableFuture);
    return resolvableFuture;
}

基本上,我开始一些异步工作,将 resolvableFuture 传递给该函数.完成异步工作后,我将从我的 ListenableWorker 传递的 resolvableFuture 对象调用以下代码:

Basically, I start some asynchronous work, passing resolvableFuture into that function. When the async work is done, I call the following on that resolvableFuture object passed from my ListenableWorker:

resolvableFuture.set(Result.success());

这很好用,现在看来仍然有效,但是我现在看到针对 ResolvableFuture.create()的以下棉绒错误消息:

This has worked well, and still appears to, but I'm now seeing the following lint error message against ResolvableFuture.create():

只能从相同的库组前缀中调用ResolvableFuture

ResolvableFuture can only be called from within the same library group prefix

它仍然可以编译,但是此警告使我感到困扰.现在正确的方法是什么?

It still compiles, but this warning bothers me. What is the correct way to do this now?

推荐答案

您根本不应该使用 ResolvableFuture ,更不用说WorkManager使用的内部版本了.

You shouldn't be using ResolvableFuture at all, much less the internal version used by WorkManager.

相反,您应该使用 AndroidX并发库:

androidx.concurrent:concurrent-futures:1.0.0 提供了 CallbackToFutureAdapter 类,该类是一种简约实用程序,可用于包装基于回调的代码并返回 ListenableFuture的实例

androidx.concurrent:concurrent-futures:1.0.0 provides CallbackToFutureAdapter class, a minimalistic utility that allows to wrap callback based code and return instances of ListenableFuture

您会注意到 1.0.0-beta01版本注意,甚至AndroidX并行库也已从其公共API中删除了 ResolveableFuture .

You'll note in the 1.0.0-beta01 release notes that even the AndroidX Concurrent Library has removed ResolveableFuture from its public API.

用于 CallbackToFutureAdapter 的Javadoc具有完整的示例,如下所示:

The Javadoc for CallbackToFutureAdapter has a full example of what this looks like:

public ListenableFuture<Result> startWork() {
    return CallbackToFutureAdapter.getFuture(completer -> {
         // Your method can call set() or setException() on the
         // Completer to signal completion
         startSomeAsyncStuff(completer);

         // This value is used only for debug purposes: it will be used 
         // in toString() of returned future or error cases.
         return "startSomeAsyncStuff";
    });
}

因此,您将使用 CallbackToFutureAdapter.Completer 代替您的 startSomeAsyncStuff 方法中的 ResolvableFuture .

So you'd use CallbackToFutureAdapter.Completer in place of a ResolvableFuture in your startSomeAsyncStuff method.

这篇关于WorkManager:ResolvableFuture只能在相同的库组前缀中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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