0 Replies
      Latest reply on Feb 26, 2020 8:56 AM by mplanckensteiner
      mplanckensteiner Level 1 Level 1 (0 points)

        Hello, I am having an issue in my app. I cannot continue running the timer when I go to the background. I got the message Can't end BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug. I already signed the capabilities for Background but I cannot figure out how to solve this.

         

        Here is my code:

         

            
        
            var timer = Timer()
            var teas = Teas()
            var isTimerRunning = false
            var alarmSound = AVAudioPlayer()
            var playing = false
            var initialValueOfTime = 0
           
            var teaMinutesLabel = ""
           
            private let notificationPublisher = NotificationPublisher()
           
            override func viewDidLoad() {
                super.viewDidLoad()
               
                overrideUserInterfaceStyle = .light
               
                initialValueOfTime = teas.time
                runTimer()
                timeFormatted(
            }
         
           
            //RunTimer()
            @objc func runTimer() {
               
                if isTimerRunning {
                    teas.time -= 1
                   
                    if teas.time <= 0 {
                        timer.invalidate()
                        let url = Bundle.main.url(forResource: "AlarmSound1", withExtension: "wav")
                        alarmSound = try! AVAudioPlayer(contentsOf: url!)
                        alarmSound.play()
        
                       
                        if alarmSound.isPlaying {
                            startButton.setTitle("Done", for: .normal)
                            playing = true
                        }
                    }
                }
                else {
                    teas.time = initialValueOfTime
                }
            }