How to use CompositeDisposable of RxJava 2?

前端 未结 3 526
心在旅途
心在旅途 2021-01-30 03:17

In RxJava 1, there was CompositeSubscription, but that is not present in RxJava2, There is something CompositeDisposable in rxJava2. How do I use CompositeDisposable or Disposab

相关标签:
3条回答
  • 2021-01-30 03:20

    CompositeDisposable cannot be reused after being disposed. If you want sync disposable lifecycle with Android Activity lifecycle, it is possible to correspond by making a simple wrapper.

    class AndroidDisposable {
        private var compositeDisposable: CompositeDisposable? = null
    
        fun add(disposable: Disposable) {
            if (compositeDisposable == null) {
                compositeDisposable = CompositeDisposable()
            }
            compositeDisposable?.add(disposable)
        }
    
        fun dispose() {
            compositeDisposable?.dispose()
            compositeDisposable = null
        }
    }
    

    How to use:

    class MainActivity : AppCompatActivity() {
        private disposable = AndroidDisposable()
    
        override fun onStart() {
            super.onStart()
            disposable.add(/* Some disposable */)
        }
    
        override fun onStop() {
            disposable.dispose()
            super.onStop()
        }
    }
    
    0 讨论(0)
  • 2021-01-30 03:38
    // clearing or unsubscibing
      disposables.clear(); 
    

    this place use dispose()

    0 讨论(0)
  • 2021-01-30 03:45
    private final CompositeDisposable disposables = new CompositeDisposable();
    
    
    // adding an Observable to the disposable
    disposables.add(sampleObservable()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableObserver<String>() {
                        @Override
                        public void onComplete() {
                        }
    
                        @Override
                        public void onError(Throwable e) {
                        }
    
                        @Override
                        public void onNext(String value) {
                        }
                    }));
    
        static Observable<String> sampleObservable() {
            return Observable.defer(new Callable<ObservableSource<? extends String>>() {
                @Override
                public ObservableSource<? extends String> call() throws Exception {
                    // Do some long running operation
                    SystemClock.sleep(2000);
                    return Observable.just("one", "two", "three", "four", "five");
                }
            });
        }                
    
    
    // Using clear will clear all, but can accept new disposable
    disposables.clear(); 
    // Using dispose will clear all and set isDisposed = true, so it will not accept any new disposable
    disposables.dispose(); 
    

    I have created a sample project to demonstrate the use of RxJava2. Here you can find the sample project - RxJava2-Android-Samples

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