I want to subscribe to an Observable
, but I do not care about events emitted before I subscribed.
There is a hacky way I found using skipUntil
operator:
let a: Observable<Int>
// ...
a.skipUntil(Observable.of(0)) // ...
It uses a new Observable.of(0)
to start noticing events from now. But it is kind of hacky, and I want a better way. Ideally:
a.skipUntilNow() // ...
There's no such thing as "events before I subscribed" because an Observable doesn't start emitting events until after it's been subscribed to.
For example look at the create
function.
let obs = Observable.create { observer in
// do stuff to observer
return Disposables.create()
}
That closure being passed into the create function is called every time the observable is subscribed to. In other words, each subscription gets its own series of events and each of them start from a fresh execution of the closure. There were no "events before the subscription happens."
All of the above is for the default "cold" observable. There is also the notion of "hot" observables (like Subjects) that share their events among all the subscribers, but they don't store old events unless you explicitly use one that is designed to do so.
So the solution to your problem is not to look for a hack, but instead to use the proper tool for the job.
UPDATE
Regarding your comment about using BehaviorSubject. The simple solution is to just not use it if you don't need its functionality. Use a PublishSubject instead. Another solution is to use .skip(1)
to avoid getting the currently held value.