Observable.Timer : NSTimer under the hood?

Hey folks!

I was reading Chapter 14 (the part about retrying requests, specifically where we return Observable<Int>.timer(Double(attempt + 1), scheduler: MainScheduler.instance).take(1) in the retryHandler and was wondering how Observable.Timer works under the hood.

Firstly, does it use NSTimer? I tried digging through the code, and got lost between scheduleRelative and TimerOneOffSink

Secondly, if it does, how does it manage the lifetime of the Timer objects? How does it invalidate them? (This would be especially important if you’re using them in a flatmap context, because then you’d be making new Timer objects ALL the time)

You can have a look at the source here: RxSwift/DispatchQueueConfiguration.swift at main · ReactiveX/RxSwift · GitHub

The timer object is managed as all other resources related to a subscription - they are released in the observable’s dispose block (chapter 4 of the book and others)

@codeofrobin Observable.timer is implemented using the Scheduler’s relative or periodic actions, which themselves are built with DispatchSource timers. They are as lightweight as possibly can be.

2 Likes

This topic was automatically closed after 166 days. New replies are no longer allowed.