Timer Patterns Reference
An API reference for iOS timer mechanisms: Timer, DispatchSourceTimer, Combine's Timer.publish, AsyncTimerSequence from the swift-async-algorithms package, and Task.sleep.
When to Use This Reference
Use this reference when:
- Looking up specific timer API syntax or parameters
- Understanding the
DispatchSourceTimerlifecycle state machine - Deciding between
Task.sleepand a repeating timer - Checking platform availability for timer APIs
- Debugging timer behavior with LLDB commands
Example Prompts
Questions you can ask your assistant that will draw from this reference:
- "What's the API for
Timer.publishin Combine?" - "How do I schedule a
DispatchSourceTimerwith leeway?" - "What RunLoop modes are available for
Timer?" - "How do I run a repeating async timer with
AsyncTimerSequence?" - "What's the platform availability for
AsyncTimerSequence?" - "Why won't my selector-based
Timercompile inside anasyncfunction?"
What's Covered
Timer
scheduledTimer(block and selector forms),Timer.initwithRunLoop.main.add(_:forMode:)- Which variants are unavailable from
asynccontexts tolerance, RunLoop modes (.default,.tracking,.common)invalidate()andisValid
DispatchSourceTimer
makeTimerSource, the.strictflagschedulewithdeadlinevswallDeadline- Event handlers and breaking retain cycles before
cancel() activate,suspend,resume,canceland the state machine's crash rules- Leeway, and how it is clamped after the first fire
Combine Timer
Timer.publish,autoconnect(), manualconnect()- Cancellation through
AnyCancellable ObservableObjectintegration
AsyncTimerSequence
- The swift-async-algorithms package requirement
ContinuousClockvsSuspendingClock- Stopping with task cancellation; background polling
Task.sleep
- One-shot delays vs repeating timers
- Retry with exponential backoff
Debugging and Availability
- LLDB commands for
Timer,DispatchSourceTimerand the run loop - Platform availability matrix
Documentation Scope
This page documents the timer-patterns-ref skill (in the axiom-integration suite), the API reference your assistant uses for timer syntax and lifecycle details. For decision trees, crash patterns and the SafeDispatchTimer wrapper — the "which timer should I use?" questions — see timer-patterns.
Related
- energy – timer tolerance and coalescing as an energy optimization
- energy-ref – timer efficiency APIs with WWDC code examples
- memory-debugging – the timer retain cycle as a memory leak pattern
Resources
Docs: /foundation/timer, /dispatch/dispatchsourcetimer, github.com/apple/swift-async-algorithms