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?
How to use CompositeDisposable of RxJava 2?
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();
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 defer –
Inhabitancy
@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 this –
Assr
Hello. I have one question regarding to your library github.com/amitshekhariitbhu/Fast-Android-Networking @AmitShekhar –
Freudian
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?@AmitShekhar –
Freudian
@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 CompositeDisposable –
Foolscap
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 disposable
–
Apodosis 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()
}
}
// clearing or unsubscibing
disposables.clear();
this place use dispose()
This is not a good advice, since you cannot resubscribe to an observable after dispose(). –
Huskey
© 2022 - 2024 — McMap. All rights reserved.