how to fetch data for every 5 seconds?
I making an app that logs battery value every 5 seconds. I am also doing a api get request along with it. these values are added in a csv file. my doubt is how do i make this task run continuously.
Thank you😄
1 Reply
To make your code run repeatedly with intervals you can use any kind of timer (Timer, DispatchSourceTimer, or even
for
with Task.sleep)
To make your app run in background you can use background tasks (https://developer.apple.com/documentation/uikit/uiapplication/1623031-beginbackgroundtask)
Note that running in the background indefinitely is only allowed for certain kinds of apps with certain purposes, and monitoring user activity usually not considered a worthy purpose.
Also note that waking up the device and starting making an internet request every 5 seconds will probably affect battery you're measuringApple Developer Documentation
beginBackgroundTask(expirationHandler:) | Apple Developer Documenta...
Marks the start of a task that should continue if the app enters the background.