0

I have implemented compactMap over BehaviorRelay and it works just fine:

class MyClass{

    let subject = BehaviorRelay(value: 1)

    func doSomething() {
        subject.compactMap{ $0 }.subscribe(onNext:{
            print($0)
        }).disposed(by: disposeBag)
    }

My question is if for some reason I want to cancel/stop the BehaviorRelay. There is a way to cancel/stop compactMap?

jkdev
  • 11,360
  • 15
  • 54
  • 77
user2924482
  • 8,380
  • 23
  • 89
  • 173

1 Answers1

0

The compactMap operator does nothing in this code. Just remove it.

To answer your question directly. An observable chain (the entire code from the source to the subscribe,) will continue until the source emits a completed/error event or the sink calls dispose on the disposable. In this case, a BehaviorRelay can't complete or error so the only way to shut down the chain is to call dispose() and it will shutdown the entire chain.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72