Do something every x minutes in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- Do something every x minutes in Swift
- Do Swift-based applications work on OS X 10.9/iOS 7 and lower?
- do while loop in Swift
- Document directory path of Xcode Device Simulator
- Does Android keep the .apk files? if so where?
- Does Android support near real time push notification?
- Does react-native support Multithreading and Background threading or Parallel Execution? How can we do that?
- Does Swift have a trim method on String?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.