admin管理员组

文章数量:1277311

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")
        }
    }
}

本文标签: swiftuiiOS app crash when both backgroundTask() and scenePhase in App fileStack Overflow