Run standalone Completable on background thread
Asked Answered
F

1

6

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().

Fisken answered 20/11, 2017 at 8:17 Comment(0)
I
10

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(...);

Impose answered 20/11, 2017 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.