I am trying to create a completable and run it on background thread but it's not calling Action
's run()
when I am subscribing on Schedulers.io()
Basically I want to do following through RxAndroid:
Thread t = new Thread(new Runnable() {
public void run() {
doSomething();
}
});
t.start();
Using RxAndroid I am doing following:
Completable.fromAction(new Action() {
@Override
public void run() throws Exception {
doSomething();
}
}).subscribeOn(Schedulers.io());
It's run()
method is not getting called if I do Schedulers.io()
, but it gets called if I do subscribe()
.
I am unable to find why it's running when I subscribe for Schedulers.io()
.
Stream would executed only if it has been subscribed. It means, that your completable should be subscribed in order run()
method to be executed. subscribeOn()
does not subscribe to the stream, it just tells on which thread to subscribe.
In your example just adding subscribe()
to the end would initiate run()
method to be called:
Completable.fromAction(new Action() {
@Override
public void run() throws Exception {
doSomething();
}
})
.subscribeOn(Schedulers.io())
.subscribe(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With