Timer im background Task not possible?

Hey there,


I have an App which should do something in background.


So I wrote some code in AppDelegate:


func applicationDidEnterBackground(_ application: UIApplication) {
        print("BackgroundActivity in Delagate")
            Timer.scheduledTimer(withTimeInterval: 30, repeats: true) { timer in
                //Do something
                } else {
                    //Do something different
                }
            }
    }


Now, every time if the application enter background I get this error in console:

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 tried to fix it with using

DispatchQueue.global(qos: .background).async {
            self.BackgroundActivity()
            DispatchQueue.main.async {
                print("Hello")
            }
        }

in viewDidLoad() of the initial view controller to start the background task already if the application is in foreground. The tast does not work and when I go to background I get the same error.


Anyone an idea what to do?


Thanks a lot!

I’ll respond in your specific thread.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Timer im background Task not possible?
 
 
Q