1

Given this simple Observer and subscriber combo which simply observes a file IO and then updates in the subscriber based on the value:

Observable.just(preferences.getBoolean(C"vibrate", false))
            .subscribeOn(Schedulers.io())//observe on new thread
            .observeOn(AndroidSchedulers.mainThread()) //subscribe(listen) on main thread
            .subscribe(new Action1<Boolean>() {
                @Override
                public void call(Boolean shouldVibrate) {
                    if (shouldVibrate)
                        Toast.makeText(context,"i should vibrate now",Toast.SHORT).show();
                }

            });

My question is, does the observer block until write is complete ? Even though i specify another thread (IO thread) does this observer still block once called by the subscriber ?

4
  • You're considering preferences.getBoolean to be the "file IO" here? Commented Jan 21, 2015 at 17:00
  • I think these questions might help? Commented Jan 21, 2015 at 17:03
  • yes its a file IO since it writes to the disk. It actually is just an example i was doing. Commented Jan 21, 2015 at 19:47
  • Thanks for the link. so in my case it would not block since its the same observer im using right ? Commented Jan 21, 2015 at 19:51

1 Answer 1

1

Your Observer won't block as your operation be done BEFORE you subscribe to your Observer (so, it will just emit your value)

Regarding your code (java 8 style for simplicity) :

Observable.just(preferences.getBoolean("vibrate", false))
        .subscribeOn(Schedulers.io())//observe on new thread
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(shouldVibrate) -> {
                if (shouldVibrate) {
                    Toast.makeText(context,"i should vibrate now",Toast.SHORT).show();
                }
        });

You can extract the call as a variable preferences.getBoolean(C"vibrate", false) :

Boolean vibrate = preferences.getBoolean("vibrate", false);
Observable.just(vibrate)
        .subscribeOn(Schedulers.io())//observe on new thread
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(shouldVibrate) -> {
                if (shouldVibrate) {
                    Toast.makeText(context,"i should vibrate now",Toast.SHORT).show();
                }
        });

As you can see, the vibrate will be computed before and then the main thread will be blocked.

You should write a "lazy" call to your method using Observable.create method

Observable.create(sub -> {
              Boolean vibrate = preferences.getBoolean("vibrate", false);
              sub.onNext(vibrate);
              sub.onCompleted();

         })
        .subscribeOn(Schedulers.io())//observe on new thread
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(shouldVibrate) -> {
                if (shouldVibrate) {
                    Toast.makeText(context,"i should vibrate now",Toast.SHORT).show();
                }
        });

With this code, the Boolean vibrate = preferences.getBoolean("vibrate", false); will be called only when you'll subscribe to your Observer.

(regarding to your if, in your subscriber, you can change it with a filter)

        Observable.create(sub -> {
              Boolean vibrate = preferences.getBoolean("vibrate", false);
              sub.onNext(vibrate);
              sub.onCompleted();

         })
        .filter((value) -> value)
        .subscribeOn(Schedulers.io())//observe on new thread
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(shouldVibrate) -> {
                    Toast.makeText(context,"i should vibrate now",Toast.SHORT).show();
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.