user2924482
user2924482

Reputation: 9120

RxSwift: compactMap never executed

I'm trying to implement compactMap on RxSwift but it seems like is never executed.

Here is my code:

class MyClass{

    var disposeBag = DisposeBag()
    let subject = BehaviorRelay(value: 1)

    func doSomething() {
        Observable.from(optional: subject).compactMap{  $0

        }.subscribe( onNext:{
            print($0)
        }).disposed(by: disposeBag)
        subject.accept(2)
        subject.accept(4)
        subject.accept(5)
        subject.accept(8)
    }
}

When I change the value on subject the compactMap never gets called. Why not?

Upvotes: 0

Views: 630

Answers (1)

Daniel T.
Daniel T.

Reputation: 33967

You are creating an Observable<BehaviorRelay<Int>> by using the from operator which only emits one value (the behavior relay itself) and then completes. The accept calls are being ignored because nothing is subscribing to the behavior relay itself.

I think you need to step back and figure out what you are trying to accomplish, and then read the documentation on the operators to find one that does what you need.

Upvotes: 1

Related Questions