Open SwiftData and TipKit stores after didFinishLaunching on iOS

TestFlight crashpoint "_hasTableWithName: + 12" (0xdead10cc) kills the
app ~1s after launch while ModelContainer.init runs WAL recovery from
YatteeApp.init(), before UIApplicationMain. That init also runs during
prewarm launches that iOS can suspend, and the suspended process holds
the store's SQLite lock. In some reports TipKit was opening its own
store from Tips.configure at the same time.

On iOS, DataManager now opens its store lazily, and AppDelegate's
didFinishLaunching configures TipKit and opens the store inside an
OpenDataStore background task. macOS and tvOS keep opening at init.
In-memory stores (tests, previews) open immediately.

Instrumentation: launch logs record prewarm, launch options, whether
the background task was granted, and open duration and trigger. Debug
iOS builds assert before opening if anything touches the store ahead
of didFinishLaunching.

This is a mitigation, not a confirmed fix; watch the crashpoint in the
next build. Moving the store out of the app group is deliberately not
done.
This commit is contained in:
Arkadiusz Fal
2026-10-31 19:50:30 +01:00
parent 03599d33b4
commit 8ee5d81a99
3 changed files with 190 additions and 49 deletions

View File

@@ -16,6 +16,39 @@ class AppDelegate: NSObject, UIApplicationDelegate {
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
// Open the SwiftData stores (TipKit's and the app's) here rather than in
// `YatteeApp.init()`, which also runs during prewarm launches that iOS
// suspends before `UIApplicationMain` (0xdead10cc if an SQLite lock is
// held). The assertion keeps a background launch running until the
// opens, including any WAL recovery, have finished and released locks.
DataManager.didFinishLaunching = true
var storeTask: UIBackgroundTaskIdentifier = .invalid
storeTask = application.beginBackgroundTask(withName: "OpenDataStore") {
LoggingService.shared.warning("Background task OpenDataStore expired", category: .diagnostics)
if storeTask != .invalid {
application.endBackgroundTask(storeTask)
storeTask = .invalid
}
}
LoggingService.shared.info(
"didFinishLaunching",
category: .diagnostics,
details: "applicationState: \(application.applicationState.rawValue), "
+ "prewarm: \(ProcessInfo.processInfo.environment["ActivePrewarm"] == "1"), "
+ "launchOptions: \(launchOptions?.keys.map(\.rawValue).sorted() ?? []), "
// `.invalid` means iOS granted no background time. The stores
// are opened anyway — deferring would only move the same
// unprotected open to the first caller (e.g. a silent-push
// sync) — but the log makes that launch identifiable.
+ "assertion: \(storeTask != .invalid)"
)
YatteeApp.configureTips()
AppEnvironment.shared.dataManager.openStore(trigger: "didFinishLaunching")
if storeTask != .invalid {
application.endBackgroundTask(storeTask)
storeTask = .invalid
}
// Required for CKSyncEngine to receive silent push notifications for remote changes.
// The remote-notification background mode is already in Info.plist.
LoggingService.shared.logCloudKit("Requesting remote notification registration...")