Run void method in background

久未见 提交于 2019-12-04 16:14:55

问题


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; }

Afterwards I run:

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

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


回答1:


The fromAction() method is what you're looking for.

Completable.fromAction(this::myHeavyMethod)


来源:https://stackoverflow.com/questions/43471039/run-void-method-in-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!