โฐTimer

Combine โŸฉ Timer โŸฉ

timer.publish() returns a ConnectablePublisher. Itโ€™s a special variant of Publisher that wonโ€™t start firing upon subscription until you explicitly call its connect() method. You can also use autoconnect() which automatically connects when the first subscriber subscribes.

๐Ÿ‘‰ Ray โŸฉ Combine: Asynchronous Programming with Swift, Ch. 11: Timers

ไฝฟ็”จ .onReceive() ๅฎš็พฉ timer ไบซๆœ‰็‰นๅˆฅ็š„ๅฅฝ่™•๏ผš็•ถ view ้—œ้–‰ๅพŒ๏ผŒtimer ไนŸๆœƒ่‡ชๅ‹•ๅœๆญข๏ผŒไธ็”จๅฆๅค–ๅ‘ผๅซ .invalidate()ใ€‚่‹ฅๆ˜ฏไฝฟ็”จ .sink() ๆŽฅๆ”ถ timer ็™ผ้€็š„ value ๅ‰‡่ฆ่™•็†่จ˜ๆ†ถ้ซ”็š„ๅ•้กŒใ€‚

๐Ÿ‘‰ ๅฝผๅพ—ๆฝ˜ โŸฉ ๅˆฉ็”จ Combine ็”ข็”Ÿ่‡ชๅ‹•ๅœๆญข็š„ timer

if youโ€™re OK with your timer having a little float, you can specify some tolerance. This allows iOS to perform energy optimization, because it can fire the timer at any point between its scheduled fire time and its scheduled fire time plus the tolerance you specify. In practice this means the system can perform timer coalescing: it can push back your timer just a little so that it fires at the same time as one or more other timers, which means it can keep the CPU idling more and save battery power.

If you need to keep time strictly then leaving off the tolerance parameter will make your timer as accurate as possible, but please note that even without any tolerance the Timer class is still โ€œbest effortโ€ โ€“ the system makes no guarantee it will execute precisely.

๐Ÿ‘‰ Paul โŸฉ Triggering events repeatedly using a timer

Last updated

Was this helpful?