From eb513e87d2a88f91d2e3ae6d2ce9d4987f846ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 11 Mar 2016 17:08:31 +0100 Subject: [PATCH] Transaction observers are now added to database queues and pools --- CHANGELOG.md | 7 +++++ GRDB/Core/Database.swift | 4 +-- GRDB/Core/DatabasePool.swift | 26 ++++++++++++++++++ GRDB/Core/DatabaseQueue.swift | 26 ++++++++++++++++++ .../Contents.swift | 5 ++-- README.md | 22 ++++++++++----- TODO.md | 4 +-- .../TransactionObserverTests.swift | 27 +++++++++---------- 8 files changed, 93 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec169ebc9..d909bc3f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Release Notes ============= +## Next Release + +**Breaking Changes** + +- [Transaction observers](https://github.com/groue/GRDB.swift#database-changes-observation) are no longer added to Database instances, but to DatabaseQueue and DatabasePool. + + ## 0.48.0 Released March 10, 2016 diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index e119a9bd0..0de91fbb1 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -929,7 +929,7 @@ extension Database { /// /// Database holds weak references to its transaction observers: they are /// not retained, and stop getting notifications after they are deallocated. - public func addTransactionObserver(transactionObserver: TransactionObserverType) { + func addTransactionObserver(transactionObserver: TransactionObserverType) { preconditionValidQueue() transactionObservers.append(WeakTransactionObserver(transactionObserver)) if transactionObservers.count == 1 { @@ -938,7 +938,7 @@ extension Database { } /// Remove a transaction observer. - public func removeTransactionObserver(transactionObserver: TransactionObserverType) { + func removeTransactionObserver(transactionObserver: TransactionObserverType) { preconditionValidQueue() transactionObservers.removeFirst { $0.observer === transactionObserver } if transactionObservers.isEmpty { diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index c8e7454df..5e2960101 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -311,6 +311,32 @@ extension DatabasePool { } +// ========================================================================= +// MARK: - Transaction Observers + +extension DatabasePool { + + /// Add a transaction observer, so that it gets notified of all + /// database changes. + /// + /// Database holds weak references to its transaction observers: they are + /// not retained, and stop getting notifications after they are deallocated. + public func addTransactionObserver(transactionObserver: TransactionObserverType) { + writer.inDatabase { db in + db.addTransactionObserver(transactionObserver) + } + } + + /// Remove a transaction observer. + public func removeTransactionObserver(transactionObserver: TransactionObserverType) { + writer.inDatabase { db in + db.removeTransactionObserver(transactionObserver) + } + } + +} + + // ========================================================================= // MARK: - DatabaseReader diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index db84150e5..7fa8c1d96 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -163,6 +163,32 @@ extension DatabaseQueue { } +// ========================================================================= +// MARK: - Transaction Observers + +extension DatabaseQueue { + + /// Add a transaction observer, so that it gets notified of all + /// database changes. + /// + /// Database holds weak references to its transaction observers: they are + /// not retained, and stop getting notifications after they are deallocated. + public func addTransactionObserver(transactionObserver: TransactionObserverType) { + serializedDatabase.inDatabase { db in + db.addTransactionObserver(transactionObserver) + } + } + + /// Remove a transaction observer. + public func removeTransactionObserver(transactionObserver: TransactionObserverType) { + serializedDatabase.inDatabase { db in + db.removeTransactionObserver(transactionObserver) + } + } + +} + + // ========================================================================= // MARK: - Collations diff --git a/Playgrounds/TransactionObserver.playground/Contents.swift b/Playgrounds/TransactionObserver.playground/Contents.swift index cd2ce3dd5..c0f49a88d 100644 --- a/Playgrounds/TransactionObserver.playground/Contents.swift +++ b/Playgrounds/TransactionObserver.playground/Contents.swift @@ -53,9 +53,8 @@ class TableChangeObserver : NSObject, TransactionObserverType { // Register observer -dbQueue.inDatabase { db in - db.addTransactionObserver(TableChangeObserver()) -} +let observer = TableChangeObserver() +dbQueue.addTransactionObserver(observer) // diff --git a/README.md b/README.md index 5e4a5195f..b7c426d25 100644 --- a/README.md +++ b/README.md @@ -1088,7 +1088,7 @@ try dbQueue.inTransaction { db in A ROLLBACK statement is issued if an error is thrown within the transaction block. -If you want to insert a transaction between other database statements, and group those in a single block of code protected by a database dispatch queue, you can use the Database.inTransaction() function: +If you want to insert a transaction between other database statements, you can use the Database.inTransaction() function: ```swift try dbQueue.inDatabase { db in @@ -2479,13 +2479,11 @@ public protocol TransactionObserverType : class { } ``` -To activate a transaction observer, add it to the database: +To activate a transaction observer, add it to the database queue or pool: ```swift let observer = MyObserver() -dbQueue.inDatabase { db in - db.addTransactionObserver(observer) -} +dbQueue.addTransactionObserver(observer) ``` Database holds weak references to its transaction observers: they are not retained, and stop getting notifications after they are deallocated. @@ -2530,7 +2528,7 @@ do { } ``` -> :point_up: **Note**: all callbacks are called on the database queue. +> :point_up: **Note**: all callbacks are called on a protected dispatch queue. > > :point_up: **Note**: the databaseDidChangeWithEvent and databaseWillCommit callbacks must not touch the SQLite database. This limitation does not apply to databaseDidCommit and databaseDidRollback which can use their database argument. @@ -2542,7 +2540,17 @@ FAQ - **How do I close a database connection?** - You do not explicitely close a database connection: a connection is open when a [database queue](#database-queues) is created, and closed when there is no longer any reference to that database queue, and it gets deallocated. This principle is known as [RAII](http://c2.com/cgi/wiki?ResourceAcquisitionIsInitialization). + The short answer is: + + ```swift + dbQueue = nil + dbPool = nil + ``` + + You do not explicitely close a database connection: it is managed by a [database queue](#database-queues) or [pool](#database-pools). The connection is closed when there is no longer any reference to that database queue or pool, and it gets deallocated. This principle is known as [RAII](http://c2.com/cgi/wiki?ResourceAcquisitionIsInitialization). + + The `releaseMemory` method of DatabasePool ([documentation](#memory-management)) will actually close some connections, but it will open another one as soon as you access the database again. + - **How do I open a database stored as a resource of my application?** diff --git a/TODO.md b/TODO.md index b0ed3b7b1..766f53180 100644 --- a/TODO.md +++ b/TODO.md @@ -2,8 +2,8 @@ Person.fetchOne(dbPool, ...) Person.fetchOne(db, ...) - [X] Read-only DatabasePool -- [ ] Move TransactionObserverType to dbQueue/Pool - - [ ] Update TransactionObserver.storyboard +- [X] Move TransactionObserverType to dbQueue/Pool + - [X] Update TransactionObserver.storyboard - [ ] Update UserDefaults.storyboard - [ ] Query builder - [ ] SELECT readers.*, books.* FROM ... JOIN ... diff --git a/Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift b/Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift index da2d83a70..c2405f515 100644 --- a/Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift +++ b/Tests/Public/Core/TransactionObserver/TransactionObserverTests.swift @@ -145,8 +145,8 @@ class TransactionObserverTests: GRDBTestCase { observer = TransactionObserver() assertNoError { + dbQueue.addTransactionObserver(self.observer) try dbQueue.inDatabase { db in - db.addTransactionObserver(self.observer) try Artist.setupInDatabase(db) try Artwork.setupInDatabase(db) } @@ -631,13 +631,13 @@ class TransactionObserverTests: GRDBTestCase { func testInsertEventIsNotifiedToAllObservers() { assertNoError { + let observer1 = TransactionObserver() + let observer2 = TransactionObserver() + + dbQueue.addTransactionObserver(observer1) + dbQueue.addTransactionObserver(observer2) + try dbQueue.inDatabase { db in - let observer1 = TransactionObserver() - let observer2 = TransactionObserver() - - db.addTransactionObserver(observer1) - db.addTransactionObserver(observer2) - let artist = Artist(name: "Gerhard Richter") // @@ -667,12 +667,13 @@ class TransactionObserverTests: GRDBTestCase { let observer2 = TransactionObserver() let observer3 = TransactionObserver() observer2.commitError = NSError(domain: "foo", code: 0, userInfo: nil) + + dbQueue.addTransactionObserver(observer1) + dbQueue.addTransactionObserver(observer2) + dbQueue.addTransactionObserver(observer3) + do { try dbQueue.inTransaction { db in - db.addTransactionObserver(observer1) - db.addTransactionObserver(observer2) - db.addTransactionObserver(observer3) - do { try Artist(name: "Gerhard Richter").save(db) } catch { @@ -709,9 +710,7 @@ class TransactionObserverTests: GRDBTestCase { do { let observer = TransactionObserver(deinitBlock: { observerReleased = true }) withExtendedLifetime(observer) { - dbQueue.inDatabase { db in - db.addTransactionObserver(observer) - } + dbQueue.addTransactionObserver(observer) XCTAssertFalse(observerReleased) } }