如何在收到onNext()后自动取消订阅?
现在我使用这段代码:
rxObservable
.compose(bindToLifecycle()) // unsubscribe automatically in onPause() if method was called in onResume()
.subscribe(new Subscriber<Object>() {
...
@Override
public void onNext(Object o) {
unsubscribe();
}
});
答案 0 :(得分:23)
我想在第一个事件之后“取消订阅”,运营商take
是一种方法。
rxObservable.compose(bindToLifecycle())
.take(1)
.subscribe();
答案 1 :(得分:1)
我认为这就是你所需要的:
rxObservable.compose(bindToLifecycle())
.takeFirst(lifecycleEvent -> lifecycleEvent == LifecycleEvent.PAUSE);