In my OS X status bar app I'm using interval
function to periodically call an external api and display the result:
Observable<Int>
.interval(120.0, scheduler: MainScheduler.instance)
.startWith(-1) // to start immediately
.flatMapLatest(makeRequest) // makeRequest is (dummy: Int) -> Observable<SummaryResponse?>
.subscribeNext(setSummary)
.addDisposableTo(disposeBag)
However, if user changes the preferences in the meantime, I would like to "restart" this interval and make a new call immediately to reflect the changes (without having to wait for the next call).
What's the best way to do this?
- Store the observable as a property and set it to
nil
or call.dispose()
on it (or both) and create a new observable ? - Set
disposeBag
tonil
and create a new observable ? - Any other way?