mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Give the database to TransactionObserverType.databaseDidCommit and databaseDidRollback
This commit is contained in:
1 parent
4a8f570383
commit
1bbbc2734e
4 files changed
+17
-13
No files matched your search
@@ -16,7 +16,7 @@ class TransactionObserver : TransactionObserverType {
|
||||
events = []
|
||||
}
|
||||
|
||||
func databaseDidRollback() {
|
||||
func databaseDidRollback(db: Database) {
|
||||
lastTransactionCompletion = .Rollback
|
||||
lastCommittedEvents = []
|
||||
events = []
|
||||
|
||||
@@ -582,15 +582,19 @@ public protocol TransactionObserverType : class {
|
||||
Database changes have been committed.
|
||||
|
||||
This method is called on the database queue. It can change the database.
|
||||
|
||||
- parameter db: A Database.
|
||||
*/
|
||||
func databaseDidCommit()
|
||||
func databaseDidCommit(db: Database)
|
||||
|
||||
/**
|
||||
Database changes have been rollbacked.
|
||||
|
||||
This method is called on the database queue. It can change the database.
|
||||
|
||||
- parameter db: A Database.
|
||||
*/
|
||||
func databaseDidRollback()
|
||||
func databaseDidRollback(db: Database)
|
||||
}
|
||||
|
||||
/// Default implementations for TransactionObserverType methods.
|
||||
@@ -602,10 +606,10 @@ public extension TransactionObserverType {
|
||||
func databaseShouldCommit() -> Bool { return true }
|
||||
|
||||
/// Default implementation does nothing.
|
||||
func databaseDidCommit() { }
|
||||
func databaseDidCommit(db: Database) { }
|
||||
|
||||
/// Default implementation does nothing.
|
||||
func databaseDidRollback() { }
|
||||
func databaseDidRollback(db: Database) { }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -965,16 +965,16 @@ public protocol TransactionObserverType : class {
|
||||
func databaseShouldCommit() -> Bool
|
||||
|
||||
// Database changes have been committed.
|
||||
func databaseDidCommit()
|
||||
func databaseDidCommit(db: Database)
|
||||
|
||||
// Database changes have been rollbacked.
|
||||
func databaseDidRollback()
|
||||
func databaseDidRollback(db: Database)
|
||||
}
|
||||
```
|
||||
|
||||
Those four callbacks are all optional, and all invoked on the database queue.
|
||||
|
||||
Changes notified to `database(_:didChangeWithEvent:)` are triggered by `INSERT`, `UPDATE` and `DELETE` statements, and also by `ON DELETE` and `ON UPDATE` actions associated to [foreign keys](https://www.sqlite.org/foreignkeys.html#fk_actions).
|
||||
Changes notified to `databaseDidChangeWithEvent(_)` are triggered by `INSERT`, `UPDATE` and `DELETE` statements, and also by `ON DELETE` and `ON UPDATE` actions associated to [foreign keys](https://www.sqlite.org/foreignkeys.html#fk_actions).
|
||||
|
||||
Those changes are not actually applied until `databaseDidCommit(_)` is called. On the other side, `databaseDidRollback(_)` confirms their invalidation:
|
||||
|
||||
@@ -996,7 +996,7 @@ try dbQueue.inDatabase do { db in
|
||||
```
|
||||
|
||||
|
||||
**Warning**: `database(_:didChangeWithEvent:)` and `databaseShouldCommit(_)` *must not* read or write to the database. This limitation does not apply to `databaseDidCommit(_)` and `databaseDidRollback(_)`.
|
||||
**Warning**: `databaseDidChangeWithEvent(_)` and `databaseShouldCommit()` *must not* read or write to the database. This limitation does not apply to `databaseDidCommit(_)` and `databaseDidRollback(_)`.
|
||||
|
||||
|
||||
**Sample code**
|
||||
@@ -1012,7 +1012,7 @@ class TableChangeObserver : TransactionObserverType {
|
||||
changedTableNames.insert(event.tableName)
|
||||
}
|
||||
|
||||
func databaseDidCommit() {
|
||||
func databaseDidCommit(db: Database) {
|
||||
// Extract the names of changed tables, and reset until
|
||||
// next database event:
|
||||
let changedTableNames = self.changedTableNames
|
||||
@@ -1027,7 +1027,7 @@ class TableChangeObserver : TransactionObserverType {
|
||||
}
|
||||
}
|
||||
|
||||
func databaseDidRollback() {
|
||||
func databaseDidRollback(db: Database) {
|
||||
// Forget the names of changed tables:
|
||||
self.changedTableNames = []
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
- [ ] #2: this commit may be how stephencelis fixed it: https://github.com/stephencelis/SQLite.swift/commit/8f64e357c3a6668c5f011c91ba33be3e8d4b88d0
|
||||
- [ ] Use @warn_unused_result where applicable
|
||||
- [ ] Support for NSURL
|
||||
- [ ] TransactionObserver
|
||||
- [ ] Since databaseDidCommit() and databaseDidRollback() can touch the database, we need to provide the database instance.
|
||||
- [ ] TransactionObserverType
|
||||
- [X] Since databaseDidCommit() and databaseDidRollback() can touch the database, we need to provide the database instance.
|
||||
- [ ] databaseShouldCommit() should be renamed databaseWillCommit(), and throws. The error should obviously pop up.
|
||||
- [ ] See if TransactionObserver makes it possible to provide external data storage.
|
||||
|
||||
|
||||
Reference in new issue
Block a user