在后台运行 void 方法 [英] Run void method in background

查看:40
本文介绍了在后台运行 void 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 rxjava 在后台运行一个方法.我不在乎结果.

I want to run a method in background using rxjava. I don't care about the result.

void myHeavyMethod() { (...) }

到目前为止,我唯一的解决方案是将返回类型修改为例如布尔值.

So far the only solution I have is to modify the return type to e.g. boolean.

boolean myHeavyMethod() { (...) return true; }

之后我跑了:

Completable.defer(() -> Completable.fromCallable(this::myHeavyMethod))
        .subscribeOn(Schedulers.computation())
        .subscribe(
                () -> {},
                throwable -> Log.e(TAG, throwable.getMessage(), throwable)
        );

有没有办法保持 void 返回类型?

Is there a way to do it keeping the void return type?

推荐答案

fromAction() 方法就是你要找的.

Completable.fromAction(this::myHeavyMethod)

这篇关于在后台运行 void 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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