RxJava2 runs the void method in the background

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

void myHeavyMethod() { (...) } 

So far, the only solution I have is to change the return type, for example. boolean .

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

Then 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 this while keeping the void return type?

+8
rx-java rx-java2
source share
1 answer

The fromAction() method is what you are looking for.

 Completable.fromAction(this::myHeavyMethod) 
+17
source share

All Articles