UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Running a Timer while the App is in the Background

Forums > SwiftUI

Hello,

I created a Round Timer, where the User can customise the number of rounds, and the time of each round and break. While the app works fine while it is in the foreground, I was struggling to find a solution how I could run the app while the app is in the background. My question is: how I could run the class even when the app is in the background? And whether this is even the right approach to create such a timer or is there a better way?

I have read some stuff on the backgroundtask framework, but from what I can comprehend the framework is not applicable to this type of task. I could be wrong since the documentation often makes little sense to me. Anyway, I would be grateful for any sort of help.

Thank You

class RoundTimer: ObservableObject{

var timer = Timer()
@Published var choicesRoundTime: [(String, [Int])] = [("Hour", Array(0...23)), ("Minute", Array(0...59)), ("Second", Array(0...59))]

@Published var selectedRoundTime: [Int] = [0, 0, 0] @Published var roundCounter: Int = 0

@Published var choicesBreakTime: [(String, [Int])] = [("Hour", Array(0...23)), ("Minute", Array(0...59)), ("Second", Array(0...59))]

@Published var selectedBreakTime: [Int] = [0, 0, 0] @Published var breakCounter: Int = 0

@Published var selectedNumberOfRounds: Int = 1 @Published var numberOfRounds: Int = 1

@Published var totalCounter: Int = 0 @Published var startstop: Bool = false @Published var roundbreak: Bool = true

@Published var remainingRoundTime: Int = 0 @Published var remainingBreakTime: Int = 0 @Published var remainingTotalTime: Int = 0

let timeFormatter: DateComponentsFormatter = { let f = DateComponentsFormatter() f.allowedUnits = [.hour, .minute, .second] f.unitsStyle = .positional f.zeroFormattingBehavior = .pad return f }()

func start(){ timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in self.startstop = true self.roundCounter += 1 self.breakCounter += 1 self.totalCounter += 1 } }

func stop(){ timer.invalidate() }

func reset(){ timer.invalidate() self.roundCounter = 0 self.totalCounter = 0 self.numberOfRounds = 1 self.startstop = false self.roundbreak = true }

@objc func timerRoundDisplay() -> Int { remainingRoundTime = (3600 self.selectedRoundTime[0]) + (60 self.selectedRoundTime[1]) + self.selectedRoundTime[2] - self.roundCounter if self.startstop && self.roundbreak { if self.numberOfRounds <= self.selectedNumberOfRounds{ if remainingRoundTime < 1 { self.breakCounter = 0 self.roundbreak = false } } else { self.reset() self.startstop = false } } return remainingRoundTime }

@objc func timerBreakDisplay() -> Int { remainingBreakTime = (3600 self.selectedBreakTime[0]) + (60 self.selectedBreakTime[1]) + self.selectedBreakTime[2] - self.breakCounter if self.startstop && self.roundbreak == false { if self.numberOfRounds <= self.selectedNumberOfRounds{ if remainingBreakTime < 1 { self.roundCounter = 0 self.roundbreak = true self.numberOfRounds += 1 } } else { self.reset() self.startstop = false } } return remainingBreakTime }

@objc func displayTotalTimer()->Int { remainingTotalTime = (((3600 (self.selectedBreakTime[0] + self.selectedRoundTime[0])) + (60 (self.selectedRoundTime[1] + self.selectedBreakTime[1])) + self.selectedBreakTime[2] + self.selectedRoundTime[2]) * self.selectedNumberOfRounds) - self.totalCounter return remainingTotalTime } }

2      

Hi,

the "trick" with background timer is to not to run it. Instead you save when user started timer and when they return to the app you can calculate elapsed time ;-)

3      

The other trick is that if you want to tell the user something when the app is in the background, use a UserNotification, which can be set for a specific time. You can add sound and a message, but it will not run code, unless the user presses on the notification when it shows up.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.