mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
asyncWrite's completion handler accepts a database connection
This guarantees that the asyncWrite method provides exclusive write access for the whole duration of (transaction + result handling).
This commit is contained in:
1 parent
d0e839c2b4
commit
f03fd276f1
3 files changed
+24
-19
No files matched your search
@@ -63,8 +63,9 @@ public protocol DatabaseWriter : DatabaseReader {
|
||||
/// If the updates throw an error, the transaction is rollbacked.
|
||||
///
|
||||
/// The *completion* closure is always called with the result of the
|
||||
/// database updates. Its argument is a failure if the transaction could not
|
||||
/// be committed.
|
||||
/// database updates. Its arguments are a database connection and the
|
||||
/// result of the transaction. This result is a failure if the transaction
|
||||
/// could not be committed.
|
||||
///
|
||||
/// Eventual concurrent database updates are postponed until the transaction
|
||||
/// and the *completion* closure have completed.
|
||||
@@ -78,7 +79,7 @@ public protocol DatabaseWriter : DatabaseReader {
|
||||
/// - parameter completion: A closure that is called with the eventual
|
||||
/// transaction error.
|
||||
/// - throws: The error thrown by the updates, or by the wrapping transaction.
|
||||
func asyncWrite<T>(_ updates: @escaping (Database) throws -> T, completion: @escaping (Result<T, Error>) -> Void)
|
||||
func asyncWrite<T>(_ updates: @escaping (Database) throws -> T, completion: @escaping (Database, Result<T, Error>) -> Void)
|
||||
#endif
|
||||
|
||||
/// Asynchronously executes database updates in a protected dispatch queue,
|
||||
@@ -175,8 +176,10 @@ extension DatabaseWriter {
|
||||
/// If the updates throw an error, the transaction is rollbacked.
|
||||
///
|
||||
/// The *completion* closure is always called with the result of the
|
||||
/// database updates. Its argument is a failure if the transaction could not
|
||||
/// be committed.
|
||||
/// database updates. Its arguments are a database connection and the
|
||||
/// result of the transaction. This result is a failure if the transaction
|
||||
/// could not be committed. The completion closure is executed in a
|
||||
/// protected dispatch queue, outside of any transaction.
|
||||
///
|
||||
/// Eventual concurrent database updates are postponed until the transaction
|
||||
/// and the *completion* closure have completed.
|
||||
@@ -190,7 +193,7 @@ extension DatabaseWriter {
|
||||
/// - parameter completion: A closure that is called with the eventual
|
||||
/// transaction error.
|
||||
/// - throws: The error thrown by the updates, or by the wrapping transaction.
|
||||
public func asyncWrite<T>(_ updates: @escaping (Database) throws -> T, completion: @escaping (Result<T, Error>) -> Void) {
|
||||
public func asyncWrite<T>(_ updates: @escaping (Database) throws -> T, completion: @escaping (Database, Result<T, Error>) -> Void) {
|
||||
asyncWriteWithoutTransaction { db in
|
||||
do {
|
||||
var result: T?
|
||||
@@ -198,9 +201,9 @@ extension DatabaseWriter {
|
||||
result = try updates(db)
|
||||
return .commit
|
||||
}
|
||||
completion(.success(result!))
|
||||
completion(db, .success(result!))
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
completion(db, .failure(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -562,7 +565,7 @@ public final class AnyDatabaseWriter : DatabaseWriter {
|
||||
|
||||
#if compiler(>=5.0)
|
||||
/// :nodoc:
|
||||
public func asyncWrite<T>(_ updates: @escaping (Database) throws -> T, completion: @escaping (Result<T, Error>) -> Void) {
|
||||
public func asyncWrite<T>(_ updates: @escaping (Database) throws -> T, completion: @escaping (Database, Result<T, Error>) -> Void) {
|
||||
base.asyncWrite(updates, completion: completion)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -8304,7 +8304,7 @@ let component = MyReadOnlyComponent(reader: dbQueue)
|
||||
The `asyncRead` method can be used from any thread. It submits your database statements for asynchronous execution on a protected dispatch queue:
|
||||
|
||||
```swift
|
||||
reader.asyncRead { result: Result<Database> in
|
||||
reader.asyncRead { (result: Result<Database>) in
|
||||
try {
|
||||
let db = try result.get()
|
||||
let players = try Player.fetchAll(db)
|
||||
@@ -8329,9 +8329,9 @@ let component = MyReadOnlyComponent(reader: dbQueue)
|
||||
The `asyncWrite` method can be used from any thread. It submits your database statements for asynchronous execution on a protected dispatch queue, wrapped inside a [database transaction](#transactions-and-savepoints):
|
||||
|
||||
```swift
|
||||
writer.asyncWrite({ db: Database in
|
||||
writer.asyncWrite({ (db: Database) in
|
||||
try Player(...).insert(db)
|
||||
}, completion: { result: Result<Void, Error> in
|
||||
}, completion: { (db: Database, result: Result<Void, Error>) in
|
||||
switch result {
|
||||
case let .success:
|
||||
// handle transaction success
|
||||
@@ -8341,20 +8341,20 @@ let component = MyReadOnlyComponent(reader: dbQueue)
|
||||
})
|
||||
```
|
||||
|
||||
`asyncWrite` accepts two function arguments. The first one executes your database updates. The second one is a completion function which accepts the result of the asynchronous transaction.
|
||||
`asyncWrite` accepts two function arguments. The first one executes your database updates. The second one is a completion function which accepts a database connection and the result of the asynchronous transaction.
|
||||
|
||||
On the first unhandled error during database updates, all changes are reverted, the whole transaction is rollbacked, and the error is passed to the completion function.
|
||||
|
||||
When the transaction completes successfully, the result of the first function is contained in the standard `Result` passed to the completion function:
|
||||
|
||||
```swift
|
||||
writer.asyncWrite({ db: Database in
|
||||
writer.asyncWrite({ (db: Database) in
|
||||
try Player(...).insert(db)
|
||||
return try Player.fetchCount(db)
|
||||
}, completion: { result: Result<Int, Error> in
|
||||
}, completion: { (db: Database, result: Result<Int, Error>) in
|
||||
switch result {
|
||||
case let .success(newPlayerCount):
|
||||
// handle value and transaction success
|
||||
print("new player count: \(newPlayerCount)")
|
||||
case let .failure(error):
|
||||
// handle transaction error
|
||||
}
|
||||
@@ -8370,7 +8370,7 @@ let component = MyReadOnlyComponent(reader: dbQueue)
|
||||
The `asyncWriteWithoutTransaction` method can be used from any thread. It submits your database statements for asynchronous execution on a protected dispatch queue, outside of any transaction:
|
||||
|
||||
```swift
|
||||
writer.asyncWriteWithoutTransaction { db in
|
||||
writer.asyncWriteWithoutTransaction { (db: Database) in
|
||||
try {
|
||||
try Player(...).insert(db)
|
||||
} catch {
|
||||
|
||||
@@ -102,7 +102,8 @@ class DatabaseWriterTests : GRDBTestCase {
|
||||
// Make sure this block executes asynchronously
|
||||
semaphore.wait()
|
||||
try db.execute(sql: "CREATE TABLE testAsyncWrite (a)")
|
||||
}, completion: { result in
|
||||
}, completion: { db, result in
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
switch result {
|
||||
case .success:
|
||||
break
|
||||
@@ -132,7 +133,8 @@ class DatabaseWriterTests : GRDBTestCase {
|
||||
// Make sure this block executes asynchronously
|
||||
semaphore.wait()
|
||||
try db.execute(sql: "This is not SQL")
|
||||
}, completion: { result in
|
||||
}, completion: { db, result in
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
switch result {
|
||||
case .success:
|
||||
XCTFail("Expected error")
|
||||
|
||||
Reference in new issue
Block a user