My code looks like this:
private let myPublishSubject = PublishSubject<Bool>()
func method(input: String) -> String {
if input == something {
myPublishSubject.onNext(true)
return update(input)
}
myPublishSubject.onNext(false)
return input
}
// Output
let driver: Driver<String>
init() {
let myObservable = Observable.just("a")
driver = Observable.combineLatest(myObservable, myPublishSubject)
.map {
...
}
.asDriver()
}
Now, in debug, I figured out that driver
only subscribed and disposed of. when method
called afterward, there isn't any subscriber to myPublishSubject.onNext
.
How can I fix it?
@Aks yesterday answered it correctly: PublishSubject
streams the event as soon as it gets without waiting for any subscriber. So when the event is triggered and the Driver was not subscribed I lost that event.
Therefore I used BehaviorRelay
instead, and it solved my problem When subscribing to BehaviorRelay
, you get the last stream even if the event happen before subscriber was created.