How to unsubscribe automatically after receiving onNext() ?
For now I use this code:
rxObservable
.compose(bindToLifecycle()) // unsubscribe automatically in onPause() if method was called in onResume()
.subscribe(new Subscriber<Object>() {
...
@Override
public void onNext(Object o) {
unsubscribe();
}
});
You need to cancel your job properly via Observable::create and Observable::flatMap. And set your cancalable in Observable::create.
An Observable is analogous to a speaker that broadcasts the value. It does various tasks and generates some values. An Operator is similar to a translator in that it converts/modifies data from one form to another. An Observer is what fetches the value.
I you want to "unsubscribe" just after the first event, the operator take
is a way to go.
rxObservable.compose(bindToLifecycle())
.take(1)
.subscribe();
I think this is what you need:
rxObservable.compose(bindToLifecycle())
.takeFirst(lifecycleEvent -> lifecycleEvent == LifecycleEvent.PAUSE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With