Android RxJava, Non Blocking?

后端 未结 1 1312
一向
一向 2020-12-06 12:07

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 qui

1条回答
  •  無奈伤痛
    2020-12-06 12:30

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

    Observable observable = Observable.create(new Observable.OnSubscribe() {
      @Override
      public void call(Subscriber 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.

    0 讨论(0)
    提交回复
    热议问题