diff --git a/GRDB/Core/DatabaseRegion.swift b/GRDB/Core/DatabaseRegion.swift index 08ec26d3f..9a739e2ff 100644 --- a/GRDB/Core/DatabaseRegion.swift +++ b/GRDB/Core/DatabaseRegion.swift @@ -186,8 +186,11 @@ extension DatabaseRegion { guard let tableRegion = tableRegions[event.tableName] else { // FTS4 (and maybe other virtual tables) perform unadvertised - // changes. So let's assume the precondition is fulfilled, and - // the region is modified, just in case. + // changes. For example, an "INSERT INTO document ..." statement + // advertises an insertion in the `document` table, but the + // actual change events happen in the `document_content` shadow + // table. When such a non-advertised event happens, assume that + // the region is modified. // See https://github.com/groue/GRDB.swift/issues/620 return true } diff --git a/GRDB/Core/TransactionObserver.swift b/GRDB/Core/TransactionObserver.swift index 13cd61a2e..a2cf3dac3 100644 --- a/GRDB/Core/TransactionObserver.swift +++ b/GRDB/Core/TransactionObserver.swift @@ -283,7 +283,11 @@ class DatabaseObservationBroker { // observation will only be notified of individual events that // match one of the observed kinds. - return (observation, DatabaseEventPredicate.matching(observedKinds)) + return ( + observation, + DatabaseEventPredicate.matching( + observedKinds: observedKinds, + advertisedKinds: eventKinds)) } } @@ -1312,15 +1316,28 @@ private struct CopiedDatabasePreUpdateEventImpl: DatabasePreUpdateEventImpl { enum DatabaseEventPredicate { // Yes filter case `true` - // Only events that match one of those kinds - case matching([DatabaseEventKind]) + // Only events that match observedKinds + case matching(observedKinds: [DatabaseEventKind], advertisedKinds: [DatabaseEventKind]) func evaluate(_ event: DatabaseEventProtocol) -> Bool { switch self { case .true: return true - case .matching(let kinds): - return kinds.contains { event.matchesKind($0) } + case let .matching(observedKinds: observedKinds, advertisedKinds: advertisedKinds): + if observedKinds.contains(where: { event.matchesKind($0) }) { + return true + } + if !advertisedKinds.contains(where: { event.matchesKind($0) }) { + // FTS4 (and maybe other virtual tables) perform unadvertised + // changes. For example, an "INSERT INTO document ..." statement + // advertises an insertion in the `document` table, but the + // actual change events happen in the `document_content` shadow + // table. When such a non-advertised event happens, assume that + // the event has to be notified. + // See https://github.com/groue/GRDB.swift/issues/620 + return true + } + return false } } }