Marcin Kunert
Marcin Kunert

Reputation: 6295

Run void method in background

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?

Upvotes: 22

Views: 8308

Answers (1)

tynn
tynn

Reputation: 39863

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

Completable.fromAction(this::myHeavyMethod)

Upvotes: 44

Related Questions