diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 7dd0525fc..c9aa8ef73 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -357,7 +357,7 @@ extension DatabasePool: DatabaseReader { } } - public func asyncRead(_ value: @escaping (Result) -> Void) { + public func asyncRead(_ value: @escaping @Sendable (Result) -> Void) { guard let readerPool else { value(.failure(DatabaseError.connectionIsClosed())) return @@ -401,7 +401,7 @@ extension DatabasePool: DatabaseReader { } } - public func asyncUnsafeRead(_ value: @escaping (Result) -> Void) { + public func asyncUnsafeRead(_ value: @escaping @Sendable (Result) -> Void) { guard let readerPool else { value(.failure(DatabaseError.connectionIsClosed())) return @@ -446,7 +446,7 @@ extension DatabasePool: DatabaseReader { } } - public func concurrentRead(_ value: @escaping (Database) throws -> T) -> DatabaseFuture { + public func concurrentRead(_ value: @escaping @Sendable (Database) throws -> T) -> DatabaseFuture { // The semaphore that blocks until futureResult is defined: let futureSemaphore = DispatchSemaphore(value: 0) var futureResult: Result? = nil @@ -464,7 +464,7 @@ extension DatabasePool: DatabaseReader { } } - public func spawnConcurrentRead(_ value: @escaping (Result) -> Void) { + public func spawnConcurrentRead(_ value: @escaping @Sendable (Result) -> Void) { asyncConcurrentRead(value) } @@ -505,7 +505,7 @@ extension DatabasePool: DatabaseReader { /// ``` /// /// - parameter value: A function that accesses the database. - public func asyncConcurrentRead(_ value: @escaping (Result) -> Void) { + public func asyncConcurrentRead(_ value: @escaping @Sendable (Result) -> Void) { // Check that we're on the writer queue... writer.execute { db in // ... and that no transaction is opened. @@ -665,7 +665,9 @@ extension DatabasePool: DatabaseReader { /// /// - important: The `completion` argument is executed in a serial /// dispatch queue, so make sure you use the transaction asynchronously. - func asyncWALSnapshotTransaction(_ completion: @escaping (Result) -> Void) { + func asyncWALSnapshotTransaction( + _ completion: @escaping @Sendable (Result) -> Void) + { guard let readerPool else { completion(.failure(DatabaseError.connectionIsClosed())) return @@ -756,7 +758,7 @@ extension DatabasePool: DatabaseWriter { } } - public func asyncBarrierWriteWithoutTransaction(_ updates: @escaping (Result) -> Void) { + public func asyncBarrierWriteWithoutTransaction(_ updates: @escaping @Sendable (Result) -> Void) { guard let readerPool else { updates(.failure(DatabaseError.connectionIsClosed())) return @@ -809,7 +811,7 @@ extension DatabasePool: DatabaseWriter { try writer.reentrantSync(updates) } - public func asyncWriteWithoutTransaction(_ updates: @escaping (Database) -> Void) { + public func asyncWriteWithoutTransaction(_ updates: @escaping @Sendable (Database) -> Void) { writer.async(updates) } } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index ae763f694..20038dfbe 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -233,7 +233,7 @@ extension DatabaseQueue: DatabaseReader { } } - public func asyncRead(_ value: @escaping (Result) -> Void) { + public func asyncRead(_ value: @escaping @Sendable (Result) -> Void) { writer.async { db in defer { // Ignore error because we can not notify it. @@ -258,7 +258,7 @@ extension DatabaseQueue: DatabaseReader { try writer.sync(value) } - public func asyncUnsafeRead(_ value: @escaping (Result) -> Void) { + public func asyncUnsafeRead(_ value: @escaping @Sendable (Result) -> Void) { writer.async { value(.success($0)) } } @@ -266,7 +266,7 @@ extension DatabaseQueue: DatabaseReader { try writer.reentrantSync(value) } - public func concurrentRead(_ value: @escaping (Database) throws -> T) -> DatabaseFuture { + public func concurrentRead(_ value: @escaping @Sendable (Database) throws -> T) -> DatabaseFuture { // DatabaseQueue can't perform parallel reads. // Perform a blocking read instead. return DatabaseFuture(Result { @@ -379,7 +379,7 @@ extension DatabaseQueue: DatabaseWriter { try writer.sync(updates) } - public func asyncBarrierWriteWithoutTransaction(_ updates: @escaping (Result) -> Void) { + public func asyncBarrierWriteWithoutTransaction(_ updates: @escaping @Sendable (Result) -> Void) { writer.async { updates(.success($0)) } } @@ -425,7 +425,7 @@ extension DatabaseQueue: DatabaseWriter { try writer.reentrantSync(updates) } - public func asyncWriteWithoutTransaction(_ updates: @escaping (Database) -> Void) { + public func asyncWriteWithoutTransaction(_ updates: @escaping @Sendable (Database) -> Void) { writer.async(updates) } } diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index a07abf9ec..76f7769e4 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -213,7 +213,7 @@ public protocol DatabaseReader: AnyObject, Sendable { /// - parameter value: A closure which accesses the database. Its argument /// is a `Result` that provides the database connection, or the failure /// that would prevent establishing the read access to the database. - func asyncRead(_ value: @escaping (Result) -> Void) + func asyncRead(_ value: @escaping @Sendable (Result) -> Void) /// Executes database operations, and returns their result after they have /// finished executing. @@ -284,7 +284,7 @@ public protocol DatabaseReader: AnyObject, Sendable { /// - parameter value: A closure which accesses the database. Its argument /// is a `Result` that provides the database connection, or the failure /// that would prevent establishing the read access to the database. - func asyncUnsafeRead(_ value: @escaping (Result) -> Void) + func asyncUnsafeRead(_ value: @escaping @Sendable (Result) -> Void) /// Executes database operations, and returns their result after they have /// finished executing. @@ -677,7 +677,7 @@ extension AnyDatabaseReader: DatabaseReader { try base.read(value) } - public func asyncRead(_ value: @escaping (Result) -> Void) { + public func asyncRead(_ value: @escaping @Sendable (Result) -> Void) { base.asyncRead(value) } @@ -686,7 +686,7 @@ extension AnyDatabaseReader: DatabaseReader { try base.unsafeRead(value) } - public func asyncUnsafeRead(_ value: @escaping (Result) -> Void) { + public func asyncUnsafeRead(_ value: @escaping @Sendable (Result) -> Void) { base.asyncUnsafeRead(value) } @@ -757,7 +757,7 @@ extension DatabaseSnapshotReader { } // There is no such thing as an unsafe access to a snapshot. - public func asyncUnsafeRead(_ value: @escaping (Result) -> Void) { + public func asyncUnsafeRead(_ value: @escaping @Sendable (Result) -> Void) { asyncRead(value) } } diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index 1381c1871..aff291373 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -154,7 +154,7 @@ extension DatabaseSnapshot: DatabaseSnapshotReader { try reader.sync(block) } - public func asyncRead(_ value: @escaping (Result) -> Void) { + public func asyncRead(_ value: @escaping @Sendable (Result) -> Void) { reader.async { value(.success($0)) } } @@ -162,7 +162,7 @@ extension DatabaseSnapshot: DatabaseSnapshotReader { try reader.sync(value) } - public func asyncUnsafeRead(_ value: @escaping (Result) -> Void) { + public func asyncUnsafeRead(_ value: @escaping @Sendable (Result) -> Void) { reader.async { value(.success($0)) } } diff --git a/GRDB/Core/DatabaseSnapshotPool.swift b/GRDB/Core/DatabaseSnapshotPool.swift index c32b0b1a2..9ce970be5 100644 --- a/GRDB/Core/DatabaseSnapshotPool.swift +++ b/GRDB/Core/DatabaseSnapshotPool.swift @@ -292,7 +292,7 @@ extension DatabaseSnapshotPool: DatabaseSnapshotReader { } } - public func asyncRead(_ value: @escaping (Result) -> Void) { + public func asyncRead(_ value: @escaping @Sendable (Result) -> Void) { guard let readerPool else { value(.failure(DatabaseError.connectionIsClosed())) return diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index b4de8f6fd..15ceeabbf 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -181,7 +181,7 @@ public protocol DatabaseWriter: DatabaseReader { /// - parameter updates: A closure which accesses the database. Its argument /// is a `Result` that provides the database connection, or the failure /// that would prevent establishing the barrier access to the database. - func asyncBarrierWriteWithoutTransaction(_ updates: @escaping (Result) -> Void) + func asyncBarrierWriteWithoutTransaction(_ updates: @escaping @Sendable (Result) -> Void) /// Schedules database operations for execution, and returns immediately. /// @@ -213,7 +213,7 @@ public protocol DatabaseWriter: DatabaseReader { /// for more information. /// /// - parameter updates: A closure which accesses the database. - func asyncWriteWithoutTransaction(_ updates: @escaping (Database) -> Void) + func asyncWriteWithoutTransaction(_ updates: @escaping @Sendable (Database) -> Void) /// Executes database operations, and returns their result after they have /// finished executing. @@ -292,7 +292,7 @@ public protocol DatabaseWriter: DatabaseReader { /// returned ``DatabaseFuture`` blocks a thread. You may prefer /// ``spawnConcurrentRead(_:)`` instead. /// - parameter value: A closure which accesses the database. - func concurrentRead(_ value: @escaping (Database) throws -> T) -> DatabaseFuture + func concurrentRead(_ value: @escaping @Sendable (Database) throws -> T) -> DatabaseFuture // Exposed for RxGRDB and GRBCombine. Naming is not stabilized. /// Schedules read-only database operations for execution. @@ -340,7 +340,7 @@ public protocol DatabaseWriter: DatabaseReader { /// - parameter value: A closure which accesses the database. Its argument /// is a `Result` that provides the database connection, or the failure /// that would prevent establishing the read access to the database. - func spawnConcurrentRead(_ value: @escaping (Result) -> Void) + func spawnConcurrentRead(_ value: @escaping @Sendable (Result) -> Void) } extension DatabaseWriter { @@ -426,8 +426,8 @@ extension DatabaseWriter { /// - parameter updates: A closure which accesses the database. /// - parameter completion: A closure called with the transaction result. public func asyncWrite( - _ updates: @escaping (Database) throws -> T, - completion: @escaping (Database, Result) -> Void) + _ updates: @escaping @Sendable (Database) throws -> T, + completion: @escaping @Sendable (Database, Result) -> Void) { asyncWriteWithoutTransaction { db in do { @@ -1003,7 +1003,7 @@ extension AnyDatabaseWriter: DatabaseReader { try base.read(value) } - public func asyncRead(_ value: @escaping (Result) -> Void) { + public func asyncRead(_ value: @escaping @Sendable (Result) -> Void) { base.asyncRead(value) } @@ -1012,7 +1012,7 @@ extension AnyDatabaseWriter: DatabaseReader { try base.unsafeRead(value) } - public func asyncUnsafeRead(_ value: @escaping (Result) -> Void) { + public func asyncUnsafeRead(_ value: @escaping @Sendable (Result) -> Void) { base.asyncUnsafeRead(value) } @@ -1044,11 +1044,11 @@ extension AnyDatabaseWriter: DatabaseWriter { try base.barrierWriteWithoutTransaction(updates) } - public func asyncBarrierWriteWithoutTransaction(_ updates: @escaping (Result) -> Void) { + public func asyncBarrierWriteWithoutTransaction(_ updates: @escaping @Sendable (Result) -> Void) { base.asyncBarrierWriteWithoutTransaction(updates) } - public func asyncWriteWithoutTransaction(_ updates: @escaping (Database) -> Void) { + public func asyncWriteWithoutTransaction(_ updates: @escaping @Sendable (Database) -> Void) { base.asyncWriteWithoutTransaction(updates) } @@ -1056,11 +1056,11 @@ extension AnyDatabaseWriter: DatabaseWriter { try base.unsafeReentrantWrite(updates) } - public func concurrentRead(_ value: @escaping (Database) throws -> T) -> DatabaseFuture { + public func concurrentRead(_ value: @escaping @Sendable (Database) throws -> T) -> DatabaseFuture { base.concurrentRead(value) } - public func spawnConcurrentRead(_ value: @escaping (Result) -> Void) { + public func spawnConcurrentRead(_ value: @escaping @Sendable (Result) -> Void) { base.spawnConcurrentRead(value) } } diff --git a/GRDB/Core/SerializedDatabase.swift b/GRDB/Core/SerializedDatabase.swift index f1dfe0572..fa5f86dc0 100644 --- a/GRDB/Core/SerializedDatabase.swift +++ b/GRDB/Core/SerializedDatabase.swift @@ -223,7 +223,7 @@ final class SerializedDatabase { } /// Schedules database operations for execution, and returns immediately. - func async(_ block: @escaping (Database) -> Void) { + func async(_ block: @escaping @Sendable (Database) -> Void) { queue.async { block(self.db) self.preconditionNoUnsafeTransactionLeft(self.db) diff --git a/GRDB/Core/WALSnapshotTransaction.swift b/GRDB/Core/WALSnapshotTransaction.swift index 32eb3afca..4a2bc69e0 100644 --- a/GRDB/Core/WALSnapshotTransaction.swift +++ b/GRDB/Core/WALSnapshotTransaction.swift @@ -71,7 +71,7 @@ class WALSnapshotTransaction { /// Schedules database operations for execution, and /// returns immediately. - func asyncRead(_ value: @escaping (Database) -> Void) { + func asyncRead(_ value: @escaping @Sendable (Database) -> Void) { // We should check the validity of the snapshot, as DatabaseSnapshotPool does. reader.async(value) } diff --git a/GRDB/Migration/DatabaseMigrator.swift b/GRDB/Migration/DatabaseMigrator.swift index 9757c85b0..816e975e3 100644 --- a/GRDB/Migration/DatabaseMigrator.swift +++ b/GRDB/Migration/DatabaseMigrator.swift @@ -264,7 +264,7 @@ public struct DatabaseMigrator { /// from succeeding. public func asyncMigrate( _ writer: some DatabaseWriter, - completion: @escaping (Result) -> Void) + completion: @escaping @Sendable (Result) -> Void) { writer.asyncBarrierWriteWithoutTransaction { dbResult in do { diff --git a/GRDB/Utils/Pool.swift b/GRDB/Utils/Pool.swift index 6e1014c75..280040a7c 100644 --- a/GRDB/Utils/Pool.swift +++ b/GRDB/Utils/Pool.swift @@ -107,7 +107,9 @@ final class Pool { /// /// - important: The `execute` argument is executed in a serial dispatch /// queue, so make sure you use the element asynchronously. - func asyncGet(_ execute: @escaping (Result<(element: T, release: (PoolCompletion) -> Void), Error>) -> Void) { + func asyncGet( + _ execute: @escaping @Sendable (Result<(element: T, release: (PoolCompletion) -> Void), Error>) -> Void) + { // Inspired by https://khanlou.com/2016/04/the-GCD-handbook/ // > We wait on the semaphore in the serial queue, which means that // > we’ll have at most one blocked thread when we reach maximum @@ -172,7 +174,7 @@ final class Pool { /// Asynchronously runs the `barrier` function when no element is used, and /// before any other element is dequeued. - func asyncBarrier(execute barrier: @escaping () -> Void) { + func asyncBarrier(execute barrier: @escaping @Sendable () -> Void) { barrierQueue.async(flags: [.barrier]) { self.itemsGroup.wait() barrier() diff --git a/GRDB/ValueObservation/ValueObservationScheduler.swift b/GRDB/ValueObservation/ValueObservationScheduler.swift index 6ca3447cf..a3df36746 100644 --- a/GRDB/ValueObservation/ValueObservationScheduler.swift +++ b/GRDB/ValueObservation/ValueObservationScheduler.swift @@ -17,11 +17,11 @@ public protocol ValueObservationScheduler { /// If the result is true, then this method was called on the main thread. func immediateInitialValue() -> Bool - func schedule(_ action: @escaping () -> Void) + func schedule(_ action: @escaping @Sendable () -> Void) } extension ValueObservationScheduler { - func scheduleInitial(_ action: @escaping () -> Void) { + func scheduleInitial(_ action: @escaping @Sendable () -> Void) { if immediateInitialValue() { action() } else { @@ -42,7 +42,7 @@ public struct AsyncValueObservationScheduler: ValueObservationScheduler { public func immediateInitialValue() -> Bool { false } - public func schedule(_ action: @escaping () -> Void) { + public func schedule(_ action: @escaping @Sendable () -> Void) { queue.async(execute: action) } } @@ -86,7 +86,7 @@ public struct ImmediateValueObservationScheduler: ValueObservationScheduler { return true } - public func schedule(_ action: @escaping () -> Void) { + public func schedule(_ action: @escaping @Sendable () -> Void) { DispatchQueue.main.async(execute: action) } }