0

Hi Everyone currently I have a problem with threads in RXJava. I wanna set visible through rxjava but android throw me a this exception

"ViewRootImpl$CalledFromWrongThreadException"

Disposable disposable = Single.concat(
            getClearStorageObservable()
                    .doOnError(Timber::e)
                    .onErrorResumeNext(Single.just(false)),
            getDownloadObservable())
            .subscribeOn(schedulers().io())
            .observeOn(schedulers().ui())
            .delay(DELAY_VALUE,TimeUnit.SECONDS)
            .timeout(5, TimeUnit.SECONDS)
            .subscribe(status -> hideErrorInformation(),
                    error -> showErrorInformation()
            );
    disposables().add(disposable);
Jay
  • 19
  • 2

1 Answers1

0

You applied delay after observeOn so the flow was switched away from the UI thread. Drop observeOn and reorder the flow as follows:

Disposable disposable = Single.concat(
        getClearStorageObservable()
                .doOnError(Timber::e)
                .onErrorResumeNext(Single.just(false)),
        getDownloadObservable())
        .subscribeOn(schedulers().io())
        .timeout(5, TimeUnit.SECONDS, schedulers().ui())
        .delay(DELAY_VALUE, TimeUnit.SECONDS, schedulers().ui())
        .subscribe(status -> hideErrorInformation(),
                error -> showErrorInformation()
        );
disposables().add(disposable);
akarnokd
  • 69,132
  • 14
  • 157
  • 192
  • Thanks you bro :) idk, that i can set scheduler in delay and timeout . I have extra question to you why are you remove observeOn. Averybody told me that subscribe on is a pointer to execute operations, and observeOn is a pointer to thread that result of execution function is return. Btw sorry for my English – SchogunSdz Jan 06 '20 at 12:12
  • Because `delay` has its own scheduler that will change the thread so having an `observeOn` just before that has no practical effect. – akarnokd Jan 06 '20 at 14:55
  • if i good understood, the problem was that delay hadnt got a scheduler? – SchogunSdz Jan 06 '20 at 19:35
  • No. `delay` is very similar to `observeOn` and having them next to each other has no practical effect. Same as having two `observeOn`s right after the other. – akarnokd Jan 06 '20 at 20:04