mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Rename Database.afterCommit(_:) to Database.afterNextTransactionCommit(_:)
This commit is contained in:
1 parent
56869577d4
commit
31aea350cb
5 files changed
+139
-71
No files matched your search
+96
-38
@@ -1,15 +1,61 @@
|
||||
Release Notes
|
||||
=============
|
||||
|
||||
## Next Version
|
||||
## 1.0
|
||||
|
||||
**New**
|
||||
**GRDB 1.0 comes with API stability, enhancements, and general cleanup.**
|
||||
|
||||
- `DatabaseMigrator.migrate(_:upTo:)`: partially migrate your databases ([documentation](https://github.com/groue/GRDB.swift#migrations))
|
||||
- [Record types](https://github.com/groue/GRDB.swift#records) have better support for applications that declare lists of columns:
|
||||
|
||||
```swift
|
||||
class Player : RowConvertible, Persistable {
|
||||
let name: String
|
||||
let score: Int
|
||||
|
||||
static let databaseTableName = "players"
|
||||
|
||||
// Declare Player columns
|
||||
enum Columns {
|
||||
static let name = Column("name")
|
||||
static let score = Column("score")
|
||||
}
|
||||
|
||||
// Use columns in `init(row:)`
|
||||
init(row: Row) {
|
||||
name = row.value(Columns.name)
|
||||
score = row.value(Columns.score)
|
||||
}
|
||||
|
||||
// Use columns in the new `encode(to:)` method:
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container[Columns.name] = name
|
||||
container[Columns.score] = score
|
||||
}
|
||||
}
|
||||
|
||||
// Use columns in the query interface
|
||||
let hallOfFame = try dbQueue.inDatabase { db in
|
||||
try Player.order(Player.Columns.score.desc).limit(10).fetchAll(db)
|
||||
}
|
||||
```
|
||||
|
||||
- `Database.afterCommit(_:)`: the simplest way to handle successful transactions ([documentation](https://github.com/groue/GRDB.swift#after-commit-hook))
|
||||
|
||||
- Transaction observers can specify the extent of their database observation ([documentation](https://github.com/groue/GRDB.swift#observation-extent)):
|
||||
- [Database Observation](https://github.com/groue/GRDB.swift#database-changes-observation) has been enhanced:
|
||||
|
||||
`Database.afterNextTransactionCommit(_:)` is the simplest way to handle successful [transactions](https://github.com/groue/GRDB.swift#transactions-and-savepoints), and synchronize the database with other resources such as files, or system sensors ([documentation](https://github.com/groue/GRDB.swift#after-commit-hook)).
|
||||
|
||||
```swift
|
||||
// Make sure the database is inside a transaction
|
||||
db.inSavepoint {
|
||||
// Perform some database job
|
||||
try ...
|
||||
|
||||
// Register extra job that is only executed after database changes
|
||||
// have been committed and written to disk.
|
||||
db.afterNextTransactionCommit { ... }
|
||||
}
|
||||
```
|
||||
|
||||
On the low-level side, applications can now specify the extent of database observation ([documentation](https://github.com/groue/GRDB.swift#observation-extent)):
|
||||
|
||||
```swift
|
||||
// New!
|
||||
@@ -17,8 +63,12 @@ Release Notes
|
||||
dbQueue.add(transactionObserver: observer, extent: .databaseLifetime)
|
||||
```
|
||||
|
||||
- **`DatabaseMigrator` is easier to test**, with its `DatabaseMigrator.migrate(_:upTo:)` method which partially migrates your databases ([documentation](https://github.com/groue/GRDB.swift#migrations)).
|
||||
|
||||
**Breaking Changes**
|
||||
|
||||
GRDB 1.0 comes with breaking changes, but the good news is that they are the last (until GRDB 2.0) :sweat_smile:!
|
||||
|
||||
- `MutablePersistable`, the protocol that defines persistence methods, has been changed:
|
||||
|
||||
```diff
|
||||
@@ -28,55 +78,50 @@ Release Notes
|
||||
}
|
||||
```
|
||||
|
||||
For example:
|
||||
|
||||
```diff
|
||||
struct Player : MutablePersistable {
|
||||
let name: String
|
||||
let score: Int
|
||||
|
||||
- var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
- return [
|
||||
- "name": name,
|
||||
- "score": score,
|
||||
- ]
|
||||
- }
|
||||
+ func encode(to container: inout PersistenceContainer) {
|
||||
+ container["name"] = name
|
||||
+ container["score"] = score
|
||||
+ }
|
||||
}
|
||||
```
|
||||
|
||||
Persistence containers can handle keys of type Column:
|
||||
You have to replace the `persistentDictionary` property with the new `encode(to:)` method:
|
||||
|
||||
```swift
|
||||
struct Player : MutablePersistable {
|
||||
let name: String
|
||||
let score: Int
|
||||
|
||||
// Old
|
||||
// var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
// return [
|
||||
// "name": name,
|
||||
// "score": score,
|
||||
// ]
|
||||
// }
|
||||
|
||||
// New
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["name"] = name
|
||||
container["score"] = score
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Alternatively, you can use the Column type:
|
||||
|
||||
```swift
|
||||
struct Player : MutablePersistable {
|
||||
let name: String
|
||||
let score: Int
|
||||
|
||||
// Declare Player columns
|
||||
enum Columns {
|
||||
static let name = Column("name")
|
||||
static let score = Column("score")
|
||||
}
|
||||
|
||||
// New
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container[Columns.name] = name
|
||||
container[Columns.score] = score
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
- `TypedRequest.Fetched` associated type has been replaced by `TypedRequest.RowDecoder`, because the type of the values fetched by a typed request is not meant to be identical to the type that decode database rows.
|
||||
|
||||
```diff
|
||||
protocol TypedRequest : Request {
|
||||
- associatedtype Fetched
|
||||
+ associatedtype RowDecoder
|
||||
}
|
||||
```
|
||||
|
||||
- `Request.adapted(_:)` and `TypedRequest.adapted(_:)` now return `AdaptedRequest` and `AdaptedTypedRequest` instead of `AnyRequest` and `AnyTypedRequest`.
|
||||
|
||||
```diff
|
||||
@@ -89,8 +134,21 @@ Release Notes
|
||||
+ func adapted(_ adapter: @escaping (Database) throws -> RowAdapter) -> AdaptedTypedRequest<Self>
|
||||
}
|
||||
```
|
||||
|
||||
Unless you define custom requests, this change is unlikely to break your code base.
|
||||
|
||||
- `DatabaseCoder`, `QueryInterfaceRequest.exists`, and `QueryInterfaceRequest.contains` have been removed.
|
||||
- `TypedRequest.Fetched` associated type has been replaced by `TypedRequest.RowDecoder`, because the type of the values fetched by a typed request is not meant to be identical to the type that decode database rows.
|
||||
|
||||
```diff
|
||||
protocol TypedRequest : Request {
|
||||
- associatedtype Fetched
|
||||
+ associatedtype RowDecoder
|
||||
}
|
||||
```
|
||||
|
||||
Unless you define custom types that adopt this protocol, this change is unlikely to break your code base.
|
||||
|
||||
- `DatabaseCoder`, `QueryInterfaceRequest.exists`, and `QueryInterfaceRequest.contains` have been removed. Chances are that you didn't use those APIs.
|
||||
|
||||
|
||||
## 0.110.0
|
||||
|
||||
@@ -320,14 +320,14 @@
|
||||
5636E9BF1D22574100B9B05F /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5636E9BB1D22574100B9B05F /* Request.swift */; };
|
||||
5636E9C01D22574100B9B05F /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5636E9BB1D22574100B9B05F /* Request.swift */; };
|
||||
5636E9C11D22574100B9B05F /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5636E9BB1D22574100B9B05F /* Request.swift */; };
|
||||
564448831EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift */; };
|
||||
564448841EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift */; };
|
||||
564448851EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift */; };
|
||||
564448861EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift */; };
|
||||
564448871EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift */; };
|
||||
564448881EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift */; };
|
||||
564448891EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift */; };
|
||||
5644488A1EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift */; };
|
||||
564448831EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448841EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448851EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448861EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448871EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448881EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448891EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
5644488A1EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564A50C81BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */; };
|
||||
565029C81E914DB700615A2C /* TableMappingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565029C71E914DB700615A2C /* TableMappingTests.swift */; };
|
||||
565029C91E914DB700615A2C /* TableMappingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565029C71E914DB700615A2C /* TableMappingTests.swift */; };
|
||||
@@ -1893,7 +1893,7 @@
|
||||
563363F81C960711000BE133 /* PerformanceRealmTests.realm */ = {isa = PBXFileReference; lastKnownFileType = file; path = PerformanceRealmTests.realm; sourceTree = "<group>"; };
|
||||
5634B1061CF9B970005360B9 /* TransactionObserverSavepointsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionObserverSavepointsTests.swift; sourceTree = "<group>"; };
|
||||
5636E9BB1D22574100B9B05F /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = "<group>"; };
|
||||
564448821EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAfterCommitTests.swift; sourceTree = "<group>"; };
|
||||
564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAfterNextTransactionCommitTests.swift; sourceTree = "<group>"; };
|
||||
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseCollationTests.swift; sourceTree = "<group>"; };
|
||||
565029C71E914DB700615A2C /* TableMappingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableMappingTests.swift; sourceTree = "<group>"; };
|
||||
565490A01D5A4798005622CB /* GRDB.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = GRDB.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
@@ -2331,7 +2331,6 @@
|
||||
children = (
|
||||
5634B1061CF9B970005360B9 /* TransactionObserverSavepointsTests.swift */,
|
||||
5607EFD21BB8254800605DE3 /* TransactionObserverTests.swift */,
|
||||
564448821EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift */,
|
||||
);
|
||||
name = TransactionObserver;
|
||||
sourceTree = "<group>";
|
||||
@@ -2627,6 +2626,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56DAA2C41DE99D8D006E10C8 /* Cursor */,
|
||||
564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */,
|
||||
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */,
|
||||
56A238161B9C74A90082EB20 /* DatabaseErrorTests.swift */,
|
||||
560C97C61BFD0B8400BF8471 /* DatabaseFunctionTests.swift */,
|
||||
@@ -2643,10 +2643,10 @@
|
||||
567A80521D41350C00C7DCEC /* IndexInfoTests.swift */,
|
||||
565EFAED1D0436CE00A8FA9D /* NumericOverflowTests.swift */,
|
||||
56741EA71E66A8B3003E422D /* RequestTests.swift */,
|
||||
568068301EBBA26100EFB8AA /* SQLRequestTests.swift */,
|
||||
56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */,
|
||||
56A2381D1B9C74A90082EB20 /* Row */,
|
||||
569C1EB11CF07DDD0042627B /* SchedulingWatchdogTests.swift */,
|
||||
568068301EBBA26100EFB8AA /* SQLRequestTests.swift */,
|
||||
56A238201B9C74A90082EB20 /* Statement */,
|
||||
56EE573B1BB317B7007A6A95 /* StatementColumnConvertible */,
|
||||
5607EFD11BB8253300605DE3 /* TransactionObserver */,
|
||||
@@ -3896,7 +3896,7 @@
|
||||
56A8C2411D1918EE0096E9D4 /* FoundationNSUUIDTests.swift in Sources */,
|
||||
562206031E420EA3005860AC /* DatabasePoolBackupTests.swift in Sources */,
|
||||
5657AB571D108BA9006283EF /* FoundationNSStringTests.swift in Sources */,
|
||||
564448841EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */,
|
||||
564448841EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
560FC5B21CB031E30014AA8E /* StatementColumnConvertibleFetchTests.swift in Sources */,
|
||||
561667021D08A49900ADD404 /* FoundationNSDecimalNumberTests.swift in Sources */,
|
||||
569C1EB31CF07DDD0042627B /* SchedulingWatchdogTests.swift in Sources */,
|
||||
@@ -4130,7 +4130,7 @@
|
||||
567156221CB16729007DC145 /* StatementColumnConvertibleFetchTests.swift in Sources */,
|
||||
562206001E420EA3005860AC /* DatabasePoolBackupTests.swift in Sources */,
|
||||
567156231CB16729007DC145 /* RecordPrimaryKeyNoneTests.swift in Sources */,
|
||||
564448851EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */,
|
||||
564448851EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
567156241CB16729007DC145 /* RecordMinimalPrimaryKeyRowIDTests.swift in Sources */,
|
||||
567156251CB16729007DC145 /* RowFromDictionaryTests.swift in Sources */,
|
||||
565F03C41CE5D3AA00DE108F /* RowAdapterTests.swift in Sources */,
|
||||
@@ -4344,7 +4344,7 @@
|
||||
56AFCA321CB1AA9900F48B96 /* DatabaseQueueConcurrencyTests.swift in Sources */,
|
||||
5698ACD31DA8C2620056AF8C /* RecordPrimaryKeyHiddenRowIDTests.swift in Sources */,
|
||||
56AFCA331CB1AA9900F48B96 /* RecordMinimalPrimaryKeyRowIDTests.swift in Sources */,
|
||||
564448881EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */,
|
||||
564448881EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
56AFCA341CB1AA9900F48B96 /* RowFromDictionaryTests.swift in Sources */,
|
||||
569C1EB61CF07DDD0042627B /* SchedulingWatchdogTests.swift in Sources */,
|
||||
56DAA2D71DE99DAB006E10C8 /* DatabaseCursorTests.swift in Sources */,
|
||||
@@ -4473,7 +4473,7 @@
|
||||
56AFCA8B1CB1ABC800F48B96 /* DatabaseQueueConcurrencyTests.swift in Sources */,
|
||||
5698ACD41DA8C2620056AF8C /* RecordPrimaryKeyHiddenRowIDTests.swift in Sources */,
|
||||
56AFCA8C1CB1ABC800F48B96 /* RecordMinimalPrimaryKeyRowIDTests.swift in Sources */,
|
||||
564448891EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */,
|
||||
564448891EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
56AFCA8D1CB1ABC800F48B96 /* RowFromDictionaryTests.swift in Sources */,
|
||||
5657AB541D108BA9006283EF /* FoundationNSNumberTests.swift in Sources */,
|
||||
56DAA2D81DE99DAB006E10C8 /* DatabaseCursorTests.swift in Sources */,
|
||||
@@ -4686,7 +4686,7 @@
|
||||
56A238561B9C74A90082EB20 /* RecordPrimaryKeyNoneTests.swift in Sources */,
|
||||
563363BE1C93FD5E000BE133 /* DatabaseQueueConcurrencyTests.swift in Sources */,
|
||||
56A238501B9C74A90082EB20 /* RecordMinimalPrimaryKeyRowIDTests.swift in Sources */,
|
||||
564448871EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */,
|
||||
564448871EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
56A238461B9C74A90082EB20 /* RowFromDictionaryTests.swift in Sources */,
|
||||
569C1EB51CF07DDD0042627B /* SchedulingWatchdogTests.swift in Sources */,
|
||||
564A50C81BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift in Sources */,
|
||||
@@ -4815,7 +4815,7 @@
|
||||
56D496B31D8133E3008276D7 /* IndexInfoTests.swift in Sources */,
|
||||
5698AC961DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */,
|
||||
56D4966D1D81309E008276D7 /* RecordMinimalPrimaryKeySingleTests.swift in Sources */,
|
||||
564448831EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */,
|
||||
564448831EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
56D496811D813131008276D7 /* TransactionObserverSavepointsTests.swift in Sources */,
|
||||
56D496AB1D8132CA008276D7 /* DatabasePoolFunctionTests.swift in Sources */,
|
||||
56D496691D813086008276D7 /* QueryInterfaceExpressionsTests.swift in Sources */,
|
||||
@@ -5114,7 +5114,7 @@
|
||||
F3BA811E1CFB3063003DC1BA /* RecordMinimalPrimaryKeyRowIDTests.swift in Sources */,
|
||||
5623934C1DEDFE4500A6B01F /* IteratorCursorTests.swift in Sources */,
|
||||
F3BA80DD1CFB300E003DC1BA /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
|
||||
5644488A1EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */,
|
||||
5644488A1EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
F3BA80521CFB2B59003DC1BA /* DatabaseQueueSchemaCacheTests.swift in Sources */,
|
||||
F3BA80BE1CFB2FD1003DC1BA /* DatabasePoolReadOnlyTests.swift in Sources */,
|
||||
F3BA80C61CFB2FD8003DC1BA /* DatabaseQueueConcurrencyTests.swift in Sources */,
|
||||
@@ -5328,7 +5328,7 @@
|
||||
5698AC991DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */,
|
||||
56A8C2451D1918EF0096E9D4 /* FoundationNSUUIDTests.swift in Sources */,
|
||||
5657AB591D108BA9006283EF /* FoundationNSStringTests.swift in Sources */,
|
||||
564448861EF56B1B00DD2861 /* DatabaseAfterCommitTests.swift in Sources */,
|
||||
564448861EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
5698ACCB1DA62A2D0056AF8C /* FTS5TokenizerTests.swift in Sources */,
|
||||
56B14E821D4DAE54000BF4A3 /* RowFromDictionaryLiteralTests.swift in Sources */,
|
||||
56FF45591D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift in Sources */,
|
||||
|
||||
@@ -1838,15 +1838,23 @@ extension Database {
|
||||
/// Registers a closure to be executed after the next or current
|
||||
/// transaction completion.
|
||||
///
|
||||
/// dbQueue.inTransaction { db in
|
||||
/// db.afterNextTransactionCommit { _ in
|
||||
/// print("commit did succeed")
|
||||
/// }
|
||||
/// ...
|
||||
/// return .commit // prints "commit did succeed"
|
||||
/// }
|
||||
///
|
||||
/// If the transaction is rollbacked, the closure is not executed.
|
||||
///
|
||||
/// If the transaction is committed, the closure is executed in a protected
|
||||
/// dispatch queue, serialized will all database updates.
|
||||
public func afterCommit(_ closure: @escaping () -> ()) {
|
||||
public func afterNextTransactionCommit(_ closure: @escaping (Database) -> ()) {
|
||||
class CommitHandler : TransactionObserver {
|
||||
let closure: () -> ()
|
||||
let closure: (Database) -> ()
|
||||
|
||||
init(_ closure: @escaping () -> ()) {
|
||||
init(_ closure: @escaping (Database) -> ()) {
|
||||
self.closure = closure
|
||||
}
|
||||
|
||||
@@ -1861,7 +1869,7 @@ extension Database {
|
||||
|
||||
// On commit, run closure
|
||||
func databaseDidCommit(_ db: Database) {
|
||||
closure()
|
||||
closure(db)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4074,13 +4074,13 @@ Database observation requires that a single [database queue](#database-queues) o
|
||||
|
||||
### After Commit Hook
|
||||
|
||||
**Whenever your applications needs to perform work after a database transaction has successfully committed, use the `Database.afterCommit(_:)` method.**
|
||||
**When an application needs to make sure a database transaction has been successfully committed before it executes some work, use the `Database.afterNextTransactionCommit(_:)` method.**
|
||||
|
||||
Its closure argument is called after database changes have been successfully written to disk:
|
||||
|
||||
```swift
|
||||
dbQueue.inTransaction { db in
|
||||
db.afterCommit {
|
||||
db.afterNextTransactionCommit { db in
|
||||
print("success")
|
||||
}
|
||||
...
|
||||
@@ -4088,21 +4088,23 @@ dbQueue.inTransaction { db in
|
||||
}
|
||||
```
|
||||
|
||||
**A typical use case is the interaction of the database and other resources, such as files or system sensors.** In the example below, a [location manager](https://developer.apple.com/documentation/corelocation/cllocationmanager) starts monitoring a CLRegion if and only if it has successfully been stored in the database:
|
||||
**This "after commit hook" helps synchronizing the database with other resources, such as files, or system sensors.**
|
||||
|
||||
In the example below, a [location manager](https://developer.apple.com/documentation/corelocation/cllocationmanager) starts monitoring a CLRegion if and only if it has successfully been stored in the database:
|
||||
|
||||
```swift
|
||||
/// Inserts a region in the database, and start monitoring upon
|
||||
/// successful insertion.
|
||||
func startMonitoring(_ db: Database, region: CLRegion) throws {
|
||||
// Make sure database is inside a transaction
|
||||
try db.inSavePoint {
|
||||
try db.inSavepoint {
|
||||
|
||||
// Save the region in the database
|
||||
try insert(...)
|
||||
|
||||
// Start monitoring if and only if the insertion is
|
||||
// eventually committed
|
||||
db.afterCommit {
|
||||
db.afterNextTransactionCommit { _ in
|
||||
// locationManager prefers the main queue:
|
||||
DispatchQueue.main.async {
|
||||
locationManager.startMonitoring(for: region)
|
||||
|
||||
+5
-5
@@ -7,9 +7,9 @@ import XCTest
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
class DatabaseAfterCommitTests: GRDBTestCase {
|
||||
class DatabaseAfterNextTransactionCommitTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseAfterCommit() throws {
|
||||
func testDatabaseAfterNextTransactionCommit() throws {
|
||||
class Witness { }
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
@@ -19,7 +19,7 @@ class DatabaseAfterCommitTests: GRDBTestCase {
|
||||
do {
|
||||
let witness = Witness()
|
||||
deallocationWitness = witness
|
||||
db.afterCommit {
|
||||
db.afterNextTransactionCommit { _ in
|
||||
// use witness
|
||||
withExtendedLifetime(witness, { })
|
||||
transactionCount += 1
|
||||
@@ -37,7 +37,7 @@ class DatabaseAfterCommitTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseAfterCommitWithRollback() throws {
|
||||
func testDatabaseAfterNextTransactionCommitWithRollback() throws {
|
||||
class Witness { }
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
@@ -47,7 +47,7 @@ class DatabaseAfterCommitTests: GRDBTestCase {
|
||||
do {
|
||||
let witness = Witness()
|
||||
deallocationWitness = witness
|
||||
db.afterCommit {
|
||||
db.afterNextTransactionCommit { _ in
|
||||
// use witness
|
||||
withExtendedLifetime(witness, { })
|
||||
transactionCount += 1
|
||||
Reference in new issue
Block a user