mirror of
https://github.com/yattee/yattee.git
synced 2026-11-03 18:37:49 +00:00
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:
@@ -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...")
|
||||
|
||||
Reference in New Issue
Block a user