I've been having trouble with .backgroundTask(.appRefresh()) causing an intermittent crash when my app is launched. When the crash happens, the app flashes a darkened screen with the home screen device information (time-of-day, battery life, etc) weirdly pivoted landscape and right-justified. This screen shows for a split-second, disappears, and the app does NOT launch. Re-tapping the app icon will often re-launch the app without incident. After much trouble-shooting, I've discovered that commenting out EITHER @Environment(\.scenePhase) private var phase
or the .backgroundTask(.appRefresh()) {}
code eliminates the crash, so it's somehow caused by having both present at the same time. As you can see in the example below, it's not even necessary to actually use .scenePhase or .backgroundTask to run any other code in order to create the crash.
The following minimal reproducible example will create the crash on an actual device, although you may need to launch and quit the app in quick succession 10-20 times to see the crash. (I realize most users aren't likely to do that - sometimes the crash occurs in the actual app on the first launch. But it's an intermittent problem so may require a few testing rounds to appear.)
In addition to the code shown here, I've added the "Background Modes" capability with "Background Fetch" to "Signing & Capabilities" and added the "UpdateBadge" task to "Info" as a "Permitted background task scheduler identifier".
Any idea what could be causing the crash and how to prevent it?
struct ToyBackgroundTasksApp: App {
@Environment(\.scenePhase) private var phase
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Item.self,
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
.backgroundTask(.appRefresh("UpdateBadge")) {
print("background task")
}
}
}
Summarizing the situation:
what could be causing the crash
I believe it's a bug in SwiftUI, however, I would also encourage you to file your own bug about it. You can find more details in this thread.
how to prevent it
If you want to workaround the issue "now", then I believe that removing ".backgroundTask()" and directly using the BackgroundTask framework to register/manage your task will prevent the crash.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware