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...
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…