Fix BGTaskScheduler crash by moving registration to App.init()

Apple requires BGTaskScheduler.register() to be called during the app
launch sequence before the run loop starts. Moving it from .onAppear
(too late) to init() prevents the crash on TestFlight builds.
This commit is contained in:
Arkadiusz Fal
2026-02-19 15:05:23 +01:00
parent 5f304ceb5c
commit 357852fbd9

View File

@@ -50,6 +50,10 @@ struct YatteeApp: App {
init() { init() {
// Configure Nuke image loading pipeline // Configure Nuke image loading pipeline
ImageLoadingService.shared.configure() ImageLoadingService.shared.configure()
// Register background tasks early Apple requires BGTaskScheduler.register()
// to be called during the app launch sequence, before the run loop starts.
appEnvironment.backgroundRefreshManager.registerBackgroundTasks()
} }
var body: some Scene { var body: some Scene {
@@ -253,9 +257,6 @@ struct YatteeApp: App {
guard !backgroundTasksRegistered else { return } guard !backgroundTasksRegistered else { return }
backgroundTasksRegistered = true backgroundTasksRegistered = true
// Register background tasks
appEnvironment.backgroundRefreshManager.registerBackgroundTasks()
// If notifications are enabled, schedule the first refresh // If notifications are enabled, schedule the first refresh
#if os(iOS) #if os(iOS)
if appEnvironment.settingsManager.backgroundNotificationsEnabled { if appEnvironment.settingsManager.backgroundNotificationsEnabled {