BGTaskScheduler registerForTaskWithIdentifier

Question: the docs said, that i have to register a background-task only once with: BGTaskScheduler registerForTaskWithIdentifier This is okay and it's working fine. But: When i reboot my iPhone do I have to register my background-task again or is the registration is valid "forever" (until I delete my app)?

In my tests I get an error after rebooting my device and run my app the first time after rebooting.. This is in the logs: objc_exception_throw + 56 (objc-exception.mm:356) 2 Foundation 0x1a9102a50 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 184 (NSException.m:242) 3 BackgroundTasks 0x213ad3250 -[BGTaskScheduler _unsafe_registerForTaskWithIdentifier:usingQueue:launchHandler:] + 304 (BGTaskScheduler.m:0) 4 BackgroundTasks 0x213ad3008 -[BGTaskScheduler registerForTaskWithIdentifier:usingQueue:launchHandler:] + 128 (BGTaskScheduler.m:171)

When i reboot my iPhone do I have to register my background-task again … ?

Yes.

More accurately, you must register every time you app launches and, as your app can’t ‘survive’ across a restart, that means you must register after a restart.

The reason why you have to call this every time your app launches is that the information you supply, the Dispatch queue and the launch handler block, are tied to the current process. So each new process needs a register call.

It’s hard to say what’s trigger the error you’re seeing. It looks like you’ve tripped an assert within BGTaskScheduler. This should output some text explaining what went wrong. If you run your app from Xcode, do you see that?

If not, please launch the app outside of Xcode. That should trigger a crash report. Grab that and post it here, using the instructions from Posting a Crash Report.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hey, thank you for your detailed help. I think that's my fail, I have a View-Extension "onFirstAppear" attached to my main-view which is only called once where I do the background-task registration. It is a SwiftUI App so i have no AppDelegate "didFinishlaunching..." so I thought this is a good place therefor. Is there a good place to do the registration in a SwiftuiApp?
Do you have a recommendation where to do the registration in a SwiftUI-App? This is my code:

 var body: some Scene {
        WindowGroup {
            ContentView()
...
`.onFirstAppear {
                    BGTaskScheduler.shared.register(forTaskWithIdentifier: ID_APPREFRESH, using: nil) { task in
                        handleAppRefresh(task: task as! BGAppRefreshTask)
                    }`

Thank you very much!

I think that's my fail

Yeah, that seems likely.

When integrating APIs that predate SwiftUI, my preferred option is to use an app delegate adaptor:

@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor(MyAppDelegate.self) var appDelegate
    …
}

final class MyAppDelegate: NSObject, UIApplicationDelegate {
    …
}

That way you can be sure that your code runs at exactly the right time it would have run in a UIKit app.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

BGTaskScheduler registerForTaskWithIdentifier
 
 
Q