Rx - 如何在收到 onNext() 后自动取消订阅?

Rx - 如何在收到 onNext() 后自动取消订阅?

问题描述:

如何在收到 onNext() 后自动退订?

How to unsubscribe automatically after receiving 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 是一种方法.

I you want to "unsubscribe" just after the first event, the operator take is a way to go.

 rxObservable.compose(bindToLifecycle())
             .take(1)
             .subscribe();