fix(analytics): prevent database locks during suspension
This commit is contained in:
@@ -174,7 +174,7 @@ public struct AnalyticsRepositoryConfiguration: Sendable {
|
||||
maximumEventCount: Int = 10_000,
|
||||
maximumStoredBytes: Int = 5 * 1_024 * 1_024,
|
||||
eventRetention: TimeInterval = 34 * 24 * 60 * 60,
|
||||
busyTimeoutMilliseconds: Int32 = 2_000
|
||||
busyTimeoutMilliseconds: Int32 = 250
|
||||
) {
|
||||
self.databaseURL = databaseURL
|
||||
self.maximumEventCount = max(1, maximumEventCount)
|
||||
|
||||
@@ -96,6 +96,7 @@ public actor AnalyticsRepository {
|
||||
private let clock: any AnalyticsWallClock
|
||||
private let uuidGenerator: any AnalyticsUUIDGenerating
|
||||
private var database: SQLiteDatabase?
|
||||
private var isDatabaseAccessSuspended = false
|
||||
|
||||
public init(
|
||||
configuration: AnalyticsRepositoryConfiguration = .appGroupDefault(),
|
||||
@@ -107,6 +108,19 @@ public actor AnalyticsRepository {
|
||||
self.uuidGenerator = uuidGenerator
|
||||
}
|
||||
|
||||
/// Re-enables process-local access before foreground or BGTask work.
|
||||
public func resumeDatabaseAccess() {
|
||||
isDatabaseAccessSuspended = false
|
||||
}
|
||||
|
||||
/// Runs after earlier actor operations, then closes the process-local
|
||||
/// connection. Later fire-and-forget records become best-effort no-ops
|
||||
/// until an explicitly authorized execution window resumes access.
|
||||
public func suspendDatabaseAccess() {
|
||||
isDatabaseAccessSuspended = true
|
||||
database = nil
|
||||
}
|
||||
|
||||
/// The host calls this only after cold-start attribution has been resolved.
|
||||
/// Installation identity, FIRST_OPEN and its marker share one transaction.
|
||||
public func prepare(
|
||||
@@ -926,6 +940,7 @@ public actor AnalyticsRepository {
|
||||
// MARK: - Metadata and setup
|
||||
|
||||
private func openDatabaseIfNeeded() -> SQLiteDatabase? {
|
||||
guard !isDatabaseAccessSuspended else { return nil }
|
||||
if let database {
|
||||
return database
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user