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();
});
preferences.getBooleanto be the "file IO" here?