mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
TransactionObserver: update documentation about database event filtering
This commit is contained in:
1 parent
e5dce929bb
commit
0008ffc735
2 files changed
+52
-12
No files matched your search
+2
-1
@@ -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**
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user