1

I have two Observables and I want them both to terminate with completion event when either of them is completed. They both branch from the same sequence, but have different termination condition:

.filter.take(1)
.distinctUntilChanged.take(2)

How can I have two Observables complete together when either one completes?

duan
  • 8,515
  • 3
  • 48
  • 70
Alex Stone
  • 46,408
  • 55
  • 231
  • 407

1 Answers1

1

I think you need to manage subscriptions manually, here is an example:

var disposable1: Disposable?
var disposable2: Disposable?
disposable1 = observable.filter().take(1).subscribe(onDisposed: {
    disposable2?.dispose()
})
disposable2 = observable.distinctUntilChanged().take(2).subscribe(onDisposed: {
    disposable1?.dispose()
})
duan
  • 8,515
  • 3
  • 48
  • 70
  • I was hoping there is a way to avoid manual subscription management through the use of some operator. It just seems too error prone to do this kind of stuff by hand, especially when using UI bindings and having no subscription handle to begin with – Alex Stone Aug 10 '18 at 00:39