Do something every x minutes in Swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Swift, doing something every few minutes usually means scheduling repeating work with a timer. The right tool depends on where the code runs. For UI-driven app code, Timer is often the simplest choice. For more control over queues and precision, DispatchSourceTimer is a strong alternative. The one big caveat is that repeating timers do not turn an iOS app into a background scheduler automatically.
Use Timer on the Main Run Loop
For many app-level tasks, Timer.scheduledTimer is the easiest option.
Here the task runs every 5 minutes. Because the timer is scheduled on the current run loop, this pattern is convenient in UI apps where the main run loop is already active.
Avoid Losing the Timer
If you create a timer and do not keep a strong reference to it, it may stop being useful or become difficult to cancel cleanly. That is why the example stores it in a property.
You should also invalidate the timer when the work is no longer needed:
This is important in controllers or services whose lifecycle should not leak repeating work.
Use DispatchSourceTimer for More Control
If you want a GCD-based timer on a specific queue, use DispatchSourceTimer.
This is useful when the work should not live on the main thread.
Swift Concurrency Alternative
If you are already using Swift concurrency, a task loop can express repeating behavior clearly.
This approach is pleasant in async-heavy code, though it is still a repeating loop and still needs cancellation management.
iOS Background Limits
A repeating timer is not a guarantee that code will run every X minutes while an iPhone app sits in the background. iOS suspends background apps aggressively unless you are using a specific background mode or system-supported scheduling mechanism.
So:
- timers work normally while the app is active
- background execution is limited by platform rules
- "every five minutes forever" is not a realistic mobile app assumption
That is a platform constraint, not a timer bug.
Common Pitfalls
The most common mistake is assuming Timer will keep firing indefinitely while the app is backgrounded. On iOS, that is usually false.
Another issue is forgetting to invalidate the timer, which can keep work running longer than intended.
Some developers also schedule timers on threads without a run loop and then wonder why nothing happens. Timer depends on a run loop, while DispatchSourceTimer depends on a dispatch queue.
Finally, choose the interval carefully. A very frequent repeating task can hurt battery life and app responsiveness.
Summary
- Use
Timer.scheduledTimerfor simple repeating work in app code. - Use
DispatchSourceTimerwhen you need more queue control. - A Swift concurrency loop with
Task.sleepis another valid option in async-heavy code. - Keep a reference to the timer and stop it explicitly when needed.
- On iOS, timers do not guarantee unlimited background execution every X minutes.

