From 0008ffc7359be932f520750ffbd85a3245ad8394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 20 Jan 2017 11:13:57 +0100 Subject: [PATCH] TransactionObserver: update documentation about database event filtering --- CHANGELOG.md | 3 ++- README.md | 61 ++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 444b7c4c7..dcbb90c7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ Release Notes **New** -- `DatabaseEventKind.tableName` +- `DatabaseEventKind.tableName` makes it easier to track any change that may happen to a database table ([documentation](https://github.com/groue/GRDB.swift#filtering-database-events)) + **Breaking Changes** diff --git a/README.md b/README.md index 7b3cd80fa..32d7c0485 100644 --- a/README.md +++ b/README.md @@ -4135,25 +4135,70 @@ At first sight, this looks somewhat redundant with the checks that observers can ```swift // BAD: An inefficient way to track the "persons" table: class PersonObserver: TransactionObserver { + var personsTableModified = false + func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool { // Observe all events return true } func databaseDidChange(with event: DatabaseEvent) { - guard event.tableName == "persons" else { - return + if event.tableName == "persons" { + personsTableModified = true } - // Process change + } + + func databaseDidRollback(_ db: Database) { + // Get ready for next transaction + personsTableModified = false + } + + func databaseDidCommit(_ db: Database) { + if personsTableModified { + // Process committed changes to the persons table + } + + // Get ready for next transaction + personsTableModified = false } } ``` The `databaseDidChange` method is invoked for each insertion, deletion, and update of individual rows. When there are many changed rows, the observer will spend of a lot of time performing the same check again and again. -More, when you're interested in specific table columns, you're out of luck, because `databaseDidChange` does not know about columns: it just knows that a row has been inserted, deleted, or updated, without further detail. +Instead, filter events in the `observes(eventsOfKind:)` method. This will prevent `databaseDidChange` from being called for changes you're not interested into, and is *much more* efficient: -Instead, filter events in the `observes(eventsOfKind:)` method, as below: +```swift +// GOOD: An efficient way to track the "persons" table: +class PersonObserver: TransactionObserver { + var personsTableModified = false + + func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool { + // Only observe changes to the "persons" table. + return eventKind.tableName == "persons" + } + + func databaseDidChange(with event: DatabaseEvent) { + personsTableModified = true // Guaranteed + } + + func databaseDidRollback(_ db: Database) { + // Get ready for next transaction + personsTableModified = false + } + + func databaseDidCommit(_ db: Database) { + if personsTableModified { + // Process committed changes to the persons table + } + + // Get ready for next transaction + personsTableModified = false + } +} +``` + +The `observes(eventsOfKind:)` method is also able to inspect the columns that are about to be changed: ```swift class PersonObserver: TransactionObserver { @@ -4168,15 +4213,9 @@ class PersonObserver: TransactionObserver { return tableName == "persons" && columnNames.contains("name") } } - - func databaseDidChange(with event: DatabaseEvent) { - // Process change - } } ``` -This technique is *much more* efficient, because GRDB will apply the filter only once for each update statement, instead of once for each modified row. - ### Support for SQLite Pre-Update Hooks