How to use CompositeDisposable of RxJava 2?
Asked Answered
P

3

80

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 Disposable in RxJava2?

Peppermint answered 29/8, 2016 at 10:17 Comment(0)
A
148
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(); 

Reference article

Assr answered 29/8, 2016 at 10:18 Comment(16)
Why they removed subscription?Peppermint
@anandgaurav : They have written rxJava2 from scratch so they have created better apis.Assr
you can also use fromCallable instead of deferInhabitancy
@anandgaurav The subscription is now used by the streams and serving another purpose. The old Subscription is now Disposable.Sanctity
@AmitShekhar only DisposableLambdaObserver is showing as a suggestion in subscribeWith.Psychasthenia
@Psychasthenia I have a running example, it would be great if you try thisAssr
github.com/amitshekhariitbhu/RxJava2-Android-Samples/blob/…Assr
Hello. I have one question regarding to your library github.com/amitshekhariitbhu/Fast-Android-Networking @AmitShekharFreudian
Will your library working fine if i would be uploaded more than 20 images? Will it be hanged my app or asynchronously upload the images?@AmitShekharFreudian
@PiyushGupta: it will upload all asynchronously.Assr
if (compositeDisposable != null && compositeDisposable.isDisposed()) { compositeDisposable.dispose(); }Breland
Hi @AmitShekhar it would be great if you can help me with that ?and if i am dealing with AsyncTask ,which Observable i should make use of ?Which Observable should i use and when i should i use?Zoography
@amit could you please help me out with how to make get and post request with api using rx java. I have seen your sample . its awsome . Suppose---baseurl/user id and password as a string need to pass . and for registration need to make post with json object . I would appreciate if i get any help for both. just post me syntax with CompositeDisposableFoolscap
thanks a lot. but is this github libraies stable. i hope its wrapper of RX java . but can i usein my project.Foolscap
@MladenRakonjac Your comment should be an answer and not a comment itself. Oh, and by the way: your logic is wrong.Elusive
@AmitShekhar Great comments about the difference between .clear() and .dispose()! I did not know that using dispose() will not accept any new disposableApodosis
B
3

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()
    }
}
Bohman answered 25/3, 2020 at 20:20 Comment(0)
K
-7
// clearing or unsubscibing
  disposables.clear(); 

this place use dispose()

Koah answered 23/3, 2017 at 2:39 Comment(1)
This is not a good advice, since you cannot resubscribe to an observable after dispose().Huskey

© 2022 - 2024 — McMap. All rights reserved.