Rx - How to unsubscribe automatically after receiving onNext()?

后端 未结 2 599
逝去的感伤
逝去的感伤 2021-02-19 05:30

How to unsubscribe automatically after receiving onNext() ?

For now I use this code:

rxObservable
.compose(bindToLifecycle()) // unsubscribe automaticall         


        
相关标签:
2条回答
  • 2021-02-19 05:52

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

     rxObservable.compose(bindToLifecycle())
                 .take(1)
                 .subscribe();
    
    0 讨论(0)
  • 2021-02-19 06:10

    I think this is what you need:

     rxObservable.compose(bindToLifecycle())
                 .takeFirst(lifecycleEvent -> lifecycleEvent == LifecycleEvent.PAUSE);
    
    0 讨论(0)
提交回复
热议问题