mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Transaction observers are now added to database queues and pools
This commit is contained in:
1 parent
96ead6d1c6
commit
eb513e87d2
8 files changed
+93
-28
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -53,9 +53,8 @@ class TableChangeObserver : NSObject, TransactionObserverType {
|
||||
|
||||
// Register observer
|
||||
|
||||
dbQueue.inDatabase { db in
|
||||
db.addTransactionObserver(TableChangeObserver())
|
||||
}
|
||||
let observer = TableChangeObserver()
|
||||
dbQueue.addTransactionObserver(observer)
|
||||
|
||||
|
||||
//
|
||||
|
||||
@@ -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?**
|
||||
|
||||
|
||||
@@ -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 ...
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user