AndroidRX - run method in background

前端 未结 5 1761
独厮守ぢ
独厮守ぢ 2021-02-12 15:38

I created simple activity with infinity progres bar, and I\'am trying to run time consuming method using RxJava to prevent UI thread from blocking, but everytime UI thread is bl

5条回答
  •  别那么骄傲
    2021-02-12 16:01

    Your doHeavyStuff() executes computation on calling thread, you just wrap your result into Observable. In order to wrap computation into observable you should use defer

    Observable.defer(new Func0>() {
        @Override
        public Observable call() {
            return Observable.just(doHeavyStuff());
        }
    });
    

    then you can specify threads by subscribeOn and observeOn methods

提交回复
热议问题