AndroidRX - run method in background

前端 未结 5 1759
独厮守ぢ
独厮守ぢ 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<Observable<Integer>>() {
        @Override
        public Observable<Integer> call() {
            return Observable.just(doHeavyStuff());
        }
    });
    

    then you can specify threads by subscribeOn and observeOn methods

    0 讨论(0)
  • 2021-02-12 16:05

    Also, you can use RxJavaAsyncUtil:

    compile 'io.reactivex:rxjava-async-util:0.21.0'
    

    Code:

    Observable.fromFunc0(() -> doHeavyStuff())
    
    0 讨论(0)
  • 2021-02-12 16:08

    According to docs

    Deprecated: fromFunc0 Unnecessary now that Func0 extends Callable. Just call fromCallable(java.util.concurrent.Callable) instead.

    So you could make the call in this way:

    Observable.fromCallable(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    return someMethod();
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Object>() {
                        @Override
                        public void call(Object object) {
    
                        }
                    });
    
    0 讨论(0)
  • 2021-02-12 16:09

    kotlin use below code to work in background

     Single.fromCallable {
                      // method that run in background
    
              }
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe()
    
    0 讨论(0)
  • 2021-02-12 16:16

    With RxJava2 a possible solution is:

    Version with lambdas:

    Single.fromCallable(() -> loadInBackground())
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe((resultObject) -> { updateUi(resultObject) });
    

    Version without lambdas:

    Single.fromCallable(new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            return loadInBackground();
        }
    })
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Consumer<Object>() {
        @Override
        public void accept(Object resultObject) throws Exception {
            updateUi(resultObject);
        }
     });
    

    Example methods used above:

    private Object loadInBackground() {
        // some heavy load code
        return resultObject;
    }
    
    private void updateUi(Object resultObject) {
        // update your Views here
    }
    
    0 讨论(0)
提交回复
热议问题