Single.subscribeWith is not disposed
See original GitHub issueRxjava version:
compile "io.reactivex.rxjava2:rxjava:2.1.6"
Example:
Disposable disposable = Single.just("123")
.subscribeWith(new DisposableSingleObserver() {
@Override
public void onSuccess(Object o) {}
@Override
public void onError(Throwable e) {}
});
System.out.println("Disposed: " + disposable.isDisposed());
Output:
Disposed: false
Expected result: Disposable should be disposed.
Related issues:
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
The result of subscribe is not used - Stack Overflow
The IDE does not know what potential effects your subscription can have when it's not disposed, so it treats it as potentially unsafe....
Read more >The curious case of RxJava Disposables A.K.A. what happens ...
In this example, we are disposing the flow in Single#map which is followed by Single#flatMap . ... No values are received by the...
Read more >DisposableSingleObserver (RxJava Javadoc 2.2.21)
Returns true if this resource has been disposed. protected void, onStart(). Called once the single upstream Disposable is set via onSubscribe.
Read more >Single (RxJava Javadoc 2.2.13)
Concatenate the single values, in a non-overlapping fashion, ... action after this Single signals onSuccess or onError or gets disposed by the downstream....
Read more >RxJava 2 (Disposing Subscriptions) · Kaushik Gopal's Site
interface Publisher<T> { // return type void (not Subscription like before) ... T> observer); } // Single implements SingleSource interface SingleSource<T> ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Since
DisposableSingleObserver
implements theSingleObserver
interface as abstract methods, it can’t calldispose()
for you and you have to call that from withinonSuccess
oronComplete
. Therefore, you can either call the lambdasubscribe()
overload and get theisDisposed
consistent with the state or define your abstract wrapper overDisposableSingleObserver
.In concept, once
onSuccess
oronError
are called, the upstream is to be considered disposed thus there should be no reason to calldispose
at that point. People often need side-effects when a source terminates or gets disposed, thus they usedoOnDisposed
and calldispose
fromonSuccess
to trigger that cleanup. However, there is a more appropriate operator calleddoFinally
that executes after terminal events or dispose calls, thus you don’t have to calldispose
to cleanup a flow.ResourceSingleObserver’s JavaDoc explains it a bit better though.
Upstream’s don’t care if your
SingleObserver
implementsDisposable
because unlike 1.x,dispose()
travels upstream and never downstream. If a chain has resources, a terminal event will free those resources so you don’t have to calldispose
.just
has no resources associated.The convenience observer classes, such as
DisposableSingleObserver
are provided so one can calldispose
on it from any thread without the need to dig into the concurrency implications of such call.isDisposed
makes only sense on container types and inside operators that check if they should continue producing data or not.Checking if a flow has terminated via
isDisposed
has two discouraged uses: trying to sleep-loop or busy-loop on it or having a completely synchronous flows that terminates once the execution returns fromsubscribe()
. You have the latter case.The
DisposableSingleObserver
keeps two abstract methods,onSuccess
andonError
from SingleObserver. You implement those methods andSingle
talks to theSingleObserver
interface. There is nothing in between that would react to the method calls and makeisDisposed
return true - we don’t use aspect oriented programming in RxJava after all.So either don’t rely on
isDisposed
or write another wrapper that implementsonSuccess
andonError
, delegates some alternative abstract methods and then callsdispose()
for you to getisDisposed
return true: