I think I finally figgured it out!
This Thread discusses a similar issue, it seems to be a SwiftUI bug. When the App launches in the background from being inactive, it’s crashing when an @Environment variable is used in the root ContentView (only seems to happen for .modelContext for me).
I was able to reliably reproduce it by launching the App in the background directly.
This is what I was doing:
@main
struct MyApp: App {
var sharedModelContainer: ModelContainer = {
// ...
}()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
}
}
struct ContentView: View {
@Environment(\.modelContext) var modelContext
var body: some View {
MyView()
}
func someFunc() {
modelContext.insert(...)
}
}
I’m now passing the container to the root view as an argument and adding it there via .modelContainer(...), which seems to have fixed the issue:
@main
struct MyApp: App {
var sharedModelContainer: ModelContainer = {
// ...
}()
var body: some Scene {
WindowGroup {
ContentView(container: sharedModelContainer)
}
}
}
struct ContentView: View {
let container: ModelContainer
var body: some View {
MyView()
.modelContainer(container)
}
func someFunc() {
let modelContext = ModelContext(container)
modelContext.insert(...)
}
}
Post
Replies
Boosts
Views
Activity
Is anyone having a similar issue?
Turning off both Background Fetch and Remote notifications gets rid of the issue, so that part seems to make more sense. I want to use both, though. It doesn't feel like it has something to do with the way I set up the .backgroundTask, as it's also happening when I don't do use it at all.
What parts of the Application are run when it refreshes in the background? Why does GeometryReader and EnvironmentValues.modelContext show up in the crash report? I'm using both, but I wasn't expecting them to be a factor when running in the background.
Here's the full crashlog: crashlog.txt. Is this sufficient or should I also add a .xccrashpoint file?