Android RxJava, Non Blocking?

It was my understanding that rxjava-android performs operations on a separate thread (when provided the correct Scheduler), leading to non-blocking operations, however a quick and dirty test seems to prove this to be incorrect.

I used the following code snippets and in both, scenarios, the UI was being blocked…

  • Android + App engine: user.getUserID() is null in endpoint
  • Android: java.lang.IllegalAccessException when attempting to use a custom “Application” class
  • How to set android app version by number of build in Jenkins?
  • Configure test folder for unit testing in Android studio
  • How can I get phone serial number (IMEI)
  • How to send Android push notifications via GCM on C# .Net
  • Snippet 1

    Observable observable = Observable.create(new Observable.OnSubscribe<Object>() {
      @Override
      public void call(Subscriber<? super Object> subscriber) {
        int i = 0;
        while (i == 0) {}
        subscriber.onCompleted();
      }
    });
    observable.subscribeOn(Schedulers.newThread());
    observable.observeOn(AndroidSchedulers.mainThread());
    observable.subscribe();
    

    Snippet 2

    Observable observable = Observable.create(new Observable.OnSubscribe<Object>() {
      @Override
      public void call(Subscriber<? super Object> subscriber) {
        SystemClock.sleep(5000);
        subscriber.onCompleted();
      }
    });
    observable.subscribeOn(Schedulers.newThread());
    observable.observeOn(AndroidSchedulers.mainThread());
    observable.subscribe();
    

    Am I missing something here?

    Related posts:

    What is the difference between List and ArrayList?
    Get all full visible objects on lListView
    java.io.IOException: Unable to open sync connection! in to the Nexus
    How to set TimePicker show with format 24h
    Build Target and Min SDK version in Android
    How do I launch my app via a custom URL in an email
  • How do you integrate Robolectric, Maven, ActionBarSherlock (and preferably + IntelliJ) into an Android project?
  • How many Volley request queues should be maintained?
  • How to get the missing Wifi MAC Address in Android Marshmallow and later?
  • How do I add an onclicklistener to a button inside a listview adapter?
  • SMS registration like in the mobile app: whatsapp
  • Hosting an executable within Android application
  • One Solution collect form web for “Android RxJava, Non Blocking?”

    The mistake is that you use the wrong Observable. The correct code should be:

    Observable observable = Observable.create(new Observable.OnSubscribe<Object>() {
      @Override
      public void call(Subscriber<? super Object> subscriber) {
        int i = 0;
        while (i == 0) {}
        subscriber.onCompleted();
      }
    });
    observable.subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe();
    

    Both subscribeOn and observeOn return a new Observable which implements their functions. But the original Observable is not modified. Actually, every operator will always create a new Observable without modifying the original one.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.