Properly checking for existing BGTaskScheduler registration

What is the safest method for verify/register background tasks in an appliction? Apple docs state that trying to register a background task that’s already scheduled will crash the app. That is confirmed. Im looking to understand the best method for checking to see if a process is already registered to prevent the crash. Any help is appreciated.

Replies

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        let taskIdentifier = "com.company.myappname.refresh"
        BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifier, using: nil) { task in
            debugMsg(msg: "worked!")
        }

The code above esults in the following error: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Launch handler for task with identifier com.att.NorthStar.refresh has already been registered'

trying to understand what I'm doing wrong. I get this error on a brand new install and there are no other ".register" references in the app.

  • SOLVED: I was using the code above and the newer .backgroundTask modifier in swiftui, not knowing that was caring for the registration.

Add a Comment