0

Here is the code

        getCompositeDisposable().add(Single.fromCallable(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    URL url = new URL(pageUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    int code = connection.getResponseCode(); // <- crashes here
                    return code == 200;
                }
            })
                    .observeOn(getSchedulerProvider().io())
                    .subscribeOn(getSchedulerProvider().ui())
                    .subscribe((pageAvailable) -> {
                        boolean useCache = !pageAvailable;
                        getMvpView().loadPage(useCache, pageUrl);
                    }, Timber::e)
    );

Cant understand what is the problem here. The network code should be run on io thread and then return result to ui thread. Does this code even runs on io thread? Im confused.

  • this is not correctly ... I think you should use `Single.defer` then sunbscribe on io and then observe on ui ... so it's totally wrong – Selvin Jan 29 '21 at 13:26

1 Answers1

4

despite correctly calling observeOn(io thread)

Incorrect, your code should be:

  .subscribeOn(getSchedulerProvider().io()) //do work on the io thread
  .observeOn(getSchedulerProvider().ui()) //apply changes to ui thread

always remember that subscribeOn is where you'll be doing the work and order does matter

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • "always remember that subscribeOn is where you'll be doing the work .." - that holds true for cold obervables, however hot observables the subscribe thread has no effect on the upstream publisher's thread - you will receive these emissions downstream on the publishers thread, unless you change using `observeOn` - its important to know these differences. – Mark Feb 02 '21 at 19:21