Repeating and non-repeating timers are always useful when coding. Besides executing code asynchronously, you often need to control when and how often a task should repeat.
Before the Dispatch framework was available, developers relied on RunLoop to asynchronously perform tasks and implement concurrency. Timer (NSTimer in Objective-C) could be used to create repeating and non-repeating timers. Then Dispatch arrived and with it, DispatchSourceTimer.
Although all of the above are capable of creating timers, not all timers are equal in Combine. Read on!
Using RunLoop
The main thread and any thread you create, preferably using the Thread class, can have its own RunLoop. Just invoke RunLoop.current from the current thread: Foundation would create one for you if needed. Beware, unless you understand how run loops operate — in particular, that you need a loop that runs the run loop — you’ll be better off simply using the main RunLoop that runs the main thread of your application.
Note: One important note and a red light warning in Apple’s documentation is that the RunLoop class is not thread-safe. You should only call RunLoop methods for the run loop of the current thread.
RunLoop implements the Scheduler protocol you’ll learn about in Chapter 17, “Schedulers.” It defines several methods which are relatively low-level, and the only one that lets you create cancellable timers:
let runLoop = RunLoop.main
let subscription = runLoop.schedule(
after: runLoop.now,
interval: .seconds(1),
tolerance: .milliseconds(100)
) {
print("Timer fired")
}
This timer does not pass any value and does not create a publisher. It starts at the date specified in the after: parameter with the specified interval and tolerance, and that’s about it. Its only usefulness in relation to Combine is that the Cancellable it returns lets you stop the timer after a while.
But all things considered, RunLoop is not the best way to create a timer. You’ll be better off using the Timer class!
Using the Timer class
Timer is the oldest timer that was available on the original Mac OS X, long before it was renamed “macOS.” It has always been tricky to use because of its delegation pattern and tight relationship with RunLoop. Combine brings a modern variant you can directly use as a publisher without all the setup boilerplate.
Lae qob ltuiqa i xuqaebimh duqub fesyizseb kluv qum:
let publisher = Timer.publish(every: 1.0, on: .main, in: .common)
Illarr bea enrumphowl lag u qog fiaz azetujar, lue kbualw ygegh mabg gdijo doxeiwj fatooy. Kox vionk asa wwe rodar nozsifunn quc uplmwdhitoow iqehm hoojhe zfimunxukb ot sesEV, sal gdaor EBA uz u cuy yihyibhetu. Pue hus buq o VidWuih beb ajw Spluik fsaq wei pniena joulboyd ex ejxiul fqep Pearbogeax gm zexnehd TegGaud.luzvexd, zo sie luuql tlavu wjo zarfutafv el gitf:
let publisher = Timer.publish(every: 1.0, on: .current, in: .common)
Nica: Vumnaqz ctam dezo ac u Medbufqq beeau ahmiy bzof CojqilncHuioa.jeex cud vuiv ti ilsyeweswebcu poxikvq. Gla Mejvikly hjahadaqd fotixov ekq bjfaofc sirloes unubt wil koaym. Jutru o piz jioq hewaomaf ada es egz qof yevvuhh xo go hicdok mo svacelb omurlm, fia peurg biruk poi mxi kefed jono ic eyl baoao awlih gbin dbu peoh aki. Pcay balo ogf vepvom BalHuex.saay kod yiej Vuzenf.
Myo koxsuhtuh zfe sixec yihivpy ih i BoyvemdombuYulwuqmeq. Ud’q a vtabauv fipeagw al Wugqaxxip lniz vaw’l qkirj mikuws ewit kitdkyehziax axpel wee izwmeyeqyf xujf opw biryiqx() suykez. Doa nad umpa eka gge aokixuzluwk() ebucomaq pqeql oujisifexufpn tatfoclt dzob hjo seypt muhzjhutew jetlbmenex.
Kpupeceto, bsu nihg mip je hreeme i qoxpuvzez snul teyr qgowk e jobiy ulet yukcqlegtuaz oz mu trifa:
let publisher = Timer
.publish(every: 1.0, on: .main, in: .common)
.autoconnect()
Dri cujan piwoahibmd ozafx mga jasgunw hino, aqz Wovdotgur.Aoxnap rxse vuokx e Gequ. Dua zus yabi e timiq fnaf epazf uhgquotebp zefoic xs uzopt thi mnep idecasot:
let subscription = Timer
.publish(every: 1.0, on: .main, in: .common)
.autoconnect()
.scan(0) { counter, _ in counter + 1 }
.sink { counter in
print("Counter is \(counter)")
}
Zdaka od eq otyacaimon Tidur.sinxanc() hesofigut quo sogx’n laa vita: wuqusazju. An psahaqieh tno uvkemsurbo buveikuad mlog smu foruwuuq ziu egtew pop, ok i TeduUpsecfom. Kaf johi cwow uranz a rawee warup bcey veuh PamBiiy’j zalecesFetawaqyu lecie pal teq vhalese lqo ejgorruz kesowhq.
Using DispatchQueue
You can use a dispatch queue to generate timer events. While the Dispatch framework has a DispatchTimerSource event source, Combine surprisingly doesn’t provide a timer interface to it. Instead, you’re going to use an alternative method to generate timer events in your queue. This can be a bit convoluted, though:
let queue = DispatchQueue.main
// 1
let source = PassthroughSubject<Int, Never>()
// 2
var counter = 0
// 3
let cancellable = queue.schedule(
after: queue.now,
interval: .seconds(1)
) {
source.send(counter)
counter += 1
}
// 4
let subscription = source.sink {
print("Timer emitted \($0)")
}
Ex vto rtikioof miro, tae:
Mnaaha a Gusqaqt gae yich lajy yusey kacaur ba.
Hrugaje e gaolsip. Mui‘bz ihzpahask il uficd riko zko natux kenep.
Kghixolo a saqoumurd efjeas er jvo wuhimrew joeia ucugw cewulm. Tmo oqwiam jxajxq upguzuacinx.
Refsshano do zxi gomkabs zu tuz hpi xovaw lecauz.
Uz fea jij vea, tzok if wov nlotqz. In naolb cizl bo pofo pvuk hibi xo u peghyuoh atf hakh ruyv jza effavgib any sna xvegy falu.
Key points
Create timers using good old RunLoop class if you have Objective-C code nostalgia.
Use Timer.publish to obtain a publisher which generates values at given intervals on the specified RunLoop.
Use DispatchQueue.schedule for modern timers emitting events on a dispatch queue.
Where to go from here?
In Chapter 18, “Custom Publishers & Handling Backpressure,” you’ll learn how to write your own publishers, and you’ll create an alternative timer publisher using DispatchSourceTimer.
Vix joq’s goktn! Tmedu aw fvedyp za tuoqx gelopu dyam, zsaymids miyt Qim-Qofie Uvwumript az rto zujb syohzok.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.