I am new to RxJava and if I understand correctly the Observer
is passed the Disposable
on the onSubscribe
so it can manually stop the processing if the dispose()
has already been called.
I created the following code:
@NonNull Observable<Long> src = Observable.interval(1, TimeUnit.SECONDS);
src.subscribe(new Observer<Long>() {
private Disposable d;
@Override
public void onSubscribe(@NonNull Disposable d) {
this.d = d;
}
@Override
public void onNext(@NonNull Long aLong) {
if(!d.isDisposed()) {
System.out.println("Number onNext = " + aLong);
}
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
System.out.println("completed");
}
});
but I can't figure out how to call dispose()
for that subscription. subscribe
with passing Observer
as an argument returns void
and subscribeWith
does not accept my Observer
without compile errors.
How is this supposed to work? What am I misunderstanding here?