diff --git a/GRDB/Core/TransactionObserver.swift b/GRDB/Core/TransactionObserver.swift index 2d1a41e99..cd54c4d0d 100644 --- a/GRDB/Core/TransactionObserver.swift +++ b/GRDB/Core/TransactionObserver.swift @@ -480,8 +480,26 @@ class DatabaseObservationBroker { savepointStack.clear() if !database.isReadOnly { - for observation in transactionObservations { - observation.databaseDidCommit(database) + var remainingObservations = transactionObservations[...] + while let commitHandling = remainingObservations.first?.commitHandling { + remainingObservations.removeAll { $0.commitHandling == commitHandling } + switch commitHandling { + case .none: + break + case .detached: + let observations = transactionObservations.filter { $0.commitHandling == commitHandling } + for observation in observations { + observation.databaseDidCommit(database) + } + case .coalescedInReadOnlyTransaction: + #warning("TODO: how do we notify this error?") + try? database.isolated(readOnly: true) { + let observations = transactionObservations.filter { $0.commitHandling == commitHandling } + for observation in observations { + observation.databaseDidCommit(database) + } + } + } } } @@ -719,6 +737,7 @@ class DatabaseObservationBroker { /// /// Adopting types must be a class. public protocol TransactionObserver: AnyObject { + var commitHandling: CommitHandling { get } /// Filters database changes that should be notified the the /// databaseDidChange(with:) method. @@ -797,7 +816,21 @@ public protocol TransactionObserver: AnyObject { #endif } +public enum CommitHandling { + /// `databaseDidCommit` is not called. + case none + + /// `databaseDidCommit` is called outside of any transaction. + case detached + + /// `databaseDidCommit` is called within a read-only transaction, shared + /// with all other transaction observers. + case coalescedInReadOnlyTransaction +} + extension TransactionObserver { + var commitHandling: CommitHandling { .detached } + /// Default implementation does nothing public func databaseWillCommit() throws { } @@ -853,6 +886,7 @@ final class TransactionObservation { private weak var weakObserver: (any TransactionObserver)? private var strongObserver: (any TransactionObserver)? private var observer: (any TransactionObserver)? { strongObserver ?? weakObserver } + var commitHandling: CommitHandling { observer?.commitHandling ?? .none } fileprivate var isObserving: Bool { observer != nil diff --git a/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift b/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift index 5ccbfb6ab..67d98778a 100644 --- a/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift +++ b/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift @@ -499,11 +499,25 @@ extension ValueConcurrentObserver: TransactionObserver { } } - func databaseDidCommit(_ writerDB: Database) { + var commitHandling: CommitHandling { // Ignore transaction unless database was modified - guard observationState.isModified else { return } + guard observationState.isModified else { + return .none + } + switch trackingMode { + case .constantRegion, .constantRegionRecordedFromSelection: + // No need to share a transaction with other observers + return .detached + case .nonConstantRegionRecordedFromSelection: + // Let's share a common transaction with all observers + return .coalescedInReadOnlyTransaction + } + } + + func databaseDidCommit(_ writerDB: Database) { // Reset the isModified flag until next transaction + assert(observationState.isModified) observationState.isModified = false // Ignore transaction unless we are still notifying database events, and diff --git a/GRDB/ValueObservation/Observers/ValueSnapshotObserver.swift b/GRDB/ValueObservation/Observers/ValueSnapshotObserver.swift index d252ba770..f8b547fa1 100644 --- a/GRDB/ValueObservation/Observers/ValueSnapshotObserver.swift +++ b/GRDB/ValueObservation/Observers/ValueSnapshotObserver.swift @@ -496,11 +496,18 @@ extension ValueSnapshotObserver: TransactionObserver { } } - func databaseDidCommit(_ writerDB: Database) { + var commitHandling: CommitHandling { // Ignore transaction unless database was modified - guard observationState.isModified else { return } - + guard observationState.isModified else { + return .none + } + // No need to share a transaction with other observers + return .detached + } + + func databaseDidCommit(_ writerDB: Database) { // Reset the isModified flag until next transaction + assert(observationState.isModified) observationState.isModified = false // Ignore transaction unless we are still notifying database events, and diff --git a/GRDB/ValueObservation/Observers/ValueWriteOnlyObserver.swift b/GRDB/ValueObservation/Observers/ValueWriteOnlyObserver.swift index ba49a7449..63197e130 100644 --- a/GRDB/ValueObservation/Observers/ValueWriteOnlyObserver.swift +++ b/GRDB/ValueObservation/Observers/ValueWriteOnlyObserver.swift @@ -29,6 +29,9 @@ final class ValueWriteOnlyObserver Reducer.Fetched { + func fetch(_ db: Database, readOnly: Bool) throws -> Reducer.Fetched { try db.isolated(readOnly: readOnly) { try reducer._fetch(db) } } - func fetchRecordingObservedRegion(_ db: Database) throws -> (Reducer.Fetched, DatabaseRegion) { + func fetchRecordingObservedRegion(_ db: Database, readOnly: Bool) throws -> (Reducer.Fetched, DatabaseRegion) { var region = DatabaseRegion() let fetchedValue = try db.isolated(readOnly: readOnly) { try db.recordingSelection(®ion) { @@ -151,11 +150,11 @@ final class ValueWriteOnlyObserver