-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CancellationException: Single::takeUntil(Observable) when pressing home button #196
Comments
One other thing I noticed is that in this case:
The Additionally I created a test case in the RxLifecycle project:
The above testcase fails for a Maybe I'm missing something but I don't see why the behaviour differs, IMO a Single should behave the same as a Observable expect for the fact it always emit a single value / error. |
Ah I think I understand what's going wrong. RxLifecycle works in such a way that it completes an Observable when the bound lifecycle event happens. Since a Single can't complete without a value an error is generated. What would be better IMO is to unsubscribe from the Observable / Single when the bound lifecycle event happens. |
See: https://github.com/trello/RxLifecycle#unsubscription There's no way to actually do what you're asking for via |
Why not? You could do something like this in the
|
RxLifecycle used to do something very similar to that but it was pointed out to be inherently unsafe: ReactiveX/RxAndroid#168 (comment) You can read more about it here: https://github.com/trello/RxLifecycle/blob/1.x/CHANGELOG.md#020 |
I trust you're right :) But I've been reading the comments, I still don't fully understand why it's unsafe to unsubscribe from the source in this fashion (except from the fact that my code snippet in incomplete). I recently wrote a similar operator for showing a progress dialog. In this case I used
|
To be honest, this stuff makes my head spin, so I sort of defer to Karnok on matters like this. |
Maybe you could mention him in this thread so he can help me understand why this is unsafe? |
It is unsafe because there is no guarantee public Observable<String> someAPI() {
return Observable.just("1").delay(2, TimeUnit.SECONDS)
.compose(bindToLifecycle(this)); // <-- how conventient?!
}
someAPI()
.observeOn(Schedulers.io())
.flatMap(v -> otherAPICall())
.subscribe(...) One needs to communicate some terminal event, such as the error in this issue or an onCompleted signal to indicate the sequence is done and trigger the necessary cleanup. So instead of this type of lifecylce binding, you'd need to have your own custom fun Observable<T>.subscribeUntil(Activity bind, Action1<T> onNext, Action1<Throwable> onError, Action0 onCompleted) {
Subscription s = this.subscribe(onNext, onError, onCompleted);
bind.addDestroyListener(() -> s.unsubscribe()); // or something similar
}
fun Observable<T>.subscribeUntil(Activity bind, Observer<T> observer) {
Subscription s = this.subscribe(observer);
bind.addDestroyListener(() -> s.unsubscribe()); // or something similar
}
``` |
Thanks for the explanation @akarnokd! It makes sense that if you unsubscribe halfway in the chain you might get leaks downstream. Since I work with
|
Is there a reason this library can't provide some kind of I'm thinking instead of this: public class MyActivity extends RxActivity {
@Override
public void onResume() {
super.onResume();
myObservable
.compose(bindToLifecycle())
.subscribe(new Subscriber() {...});
}
} We would have something like this: public class MyActivity extends RxActivity {
@Override
public void onResume() {
super.onResume();
myObservable
.subscribe(bindToLifecycle(new Subscriber() {...}));
}
} Makes sense? |
You could, but then you'd be coupling the |
When I use a single I get an error when switching an app to the background.
For example (Kotlin code) I have this in a button
onClick
:If I press the Android Home button within the 2 seconds I get this error in the
onError
:java.util.concurrent.CancellationException: Single::takeUntil(Observable) - Stream was canceled before emitting a terminal event.
The expected result would be that the observable chain is unsubscribed and the
onSuccess
andonError
will never fire...The text was updated successfully, but these errors were encountered: