如何在收到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();
}
});
如果您想在第一个事件之后"取消订阅",操作符take
是一种方法。
rxObservable.compose(bindToLifecycle())
.take(1)
.subscribe();
我想这就是你需要的:
rxObservable.compose(bindToLifecycle())
.takeFirst(lifecycleEvent -> lifecycleEvent == LifecycleEvent.PAUSE);