Reputation: 10485
Suppose I have observable A
, and I am trying to create observable B
that emits two events: the first when A
emits an event, and the second 5 seconds later.
So far I have the following:
self.B = Observable.create { [unowned self] observer in
self.A.subscribe(onNext: {
observer.onNext(0)
self.delay(5) {
observer.onNext(1)
}
})
return Disposables.create()
}
This works, but I feel uncomforatble subscribing to A
from a closure. Is there a nicer way to do it?
Thanks!
Upvotes: 2
Views: 4251
Reputation: 33979
The solution is to reuse the a
observable for the delayed observable. Below is the code to do it, along with a proof of concept.
let a = button.rx.tap.asObservable()
let delay = a.delay(5.0, scheduler: MainScheduler.instance)
let b = Observable.of(a, delay).merge()
b.subscribe(onNext: {
print("foo")
}).disposed(by: bag)
Upvotes: 2