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

查看:26
本文介绍了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() 的 lint 错误消息:

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天全站免登陆