What is the best way to cancel long running operation which starts from UI through RX2 Completable?
I have the follow code:
Completable completable = Completable.create(new CompletableOnSubscribe() {
@Override
public void subscribe(@NonNull CompletableEmitter e) throws Exception {
final LongTaskManager ltm = new LongTaskManager();
ltm.doLongTask();
e.onComplete();
}
}.subscribeOn(Schedulers.computation());
Disposable disposable = completable.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableCompletableObserver() {
@Override
public void onComplete() {
//...
}
@Override
public void onError(@NonNull Throwable e) {
//...
}
});
On leaving UI
disposable.dispose()
is called.
After dispose() I won't receive events in Observer (onComplete/onError), but long running task will continue to run.
Seems that "isInterrupted" flag should be added to LongTaskManager (like as in java.lang.Thread), it should be set on leaving UI and handled in LongTaskManager#doLongTask().
But I'm not sure that it is right way while using Rx, and would like to clarify this aspects.