diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 25328f295..a6e0eae92 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -431,7 +431,7 @@ extension DatabasePool : DatabaseReader { writer.execute { db in // ... and that no transaction is opened. GRDBPrecondition(!db.isInsideTransaction, """ - concurrentRead must not be called from inside a transaction. \ + must not be called from inside a transaction. \ If this error is raised from a DatabasePool.write block, use \ DatabasePool.writeWithoutTransaction instead (and use \ transactions when needed). @@ -484,6 +484,15 @@ extension DatabasePool : DatabaseReader { } } + #if compiler(>=5.0) + /// Performs the same job as asyncConcurrentRead. + /// + /// :nodoc: + public func spawnConcurrentRead(_ block: @escaping (Result) -> Void) { + asyncConcurrentRead(block) + } + #endif + #if compiler(>=5.0) /// Asynchronously executes a read-only block in a protected dispatch queue. /// @@ -525,7 +534,7 @@ extension DatabasePool : DatabaseReader { writer.execute { db in // ... and that no transaction is opened. GRDBPrecondition(!db.isInsideTransaction, """ - concurrentRead must not be called from inside a transaction. \ + must not be called from inside a transaction. \ If this error is raised from a DatabasePool.write block, use \ DatabasePool.writeWithoutTransaction instead (and use \ transactions when needed). diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 964bfbbde..233a026f4 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -232,7 +232,7 @@ extension DatabaseQueue { // Check that we're on the writer queue... try writer.execute { db in // ... and that no transaction is opened. - GRDBPrecondition(!db.isInsideTransaction, "concurrentRead must not be called from inside a transaction.") + GRDBPrecondition(!db.isInsideTransaction, "must not be called from inside a transaction.") return try db.readOnly { try block(db) } @@ -240,6 +240,22 @@ extension DatabaseQueue { }) } + #if compiler(>=5.0) + /// Performs the same job as asyncConcurrentRead. + /// + /// :nodoc: + public func spawnConcurrentRead(_ block: @escaping (Result) -> Void) { + // Check that we're on the writer queue... + writer.execute { db in + // ... and that no transaction is opened. + GRDBPrecondition(!db.isInsideTransaction, "must not be called from inside a transaction.") + db.readOnly { + block(.success(db)) + } + } + } + #endif + // MARK: - Writing in Database /// Synchronously executes database updates in a protected dispatch queue, diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index 96b0eb4d5..1dbc6c740 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -101,8 +101,7 @@ public protocol DatabaseWriter : DatabaseReader { // MARK: - Reading from Database - /// Concurrently executes a read-only block that takes a - /// database connection. + /// Concurrently executes a read-only block in a protected dispatch queue. /// /// This method must be called from a writing dispatch queue, outside of any /// transaction. You'll get a fatal error otherwise. @@ -111,9 +110,8 @@ public protocol DatabaseWriter : DatabaseReader { /// committed state at the moment this method is called. Eventual concurrent /// database updates are *not visible* inside the block. /// - /// This method returns as soon as the isolation guarantees described above - /// are established. To access the fetched results, you call the wait() - /// method of the returned future, on any dispatch queue. + /// To access the fetched results, you call the wait() method of the + /// returned future, on any dispatch queue. /// /// In the example below, the number of players is fetched concurrently with /// the player insertion. Yet the future is guaranteed to return zero: @@ -134,6 +132,46 @@ public protocol DatabaseWriter : DatabaseReader { /// let count = try future.wait() /// } func concurrentRead(_ block: @escaping (Database) throws -> T) -> DatabaseFuture + + #if compiler(>=5.0) + // Exposed for RxGRDB and GRBCombine. Naming is not stabilized. + /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) + /// + /// Concurrently executes a read-only block in a protected dispatch queue. + /// + /// This method must be called from a writing dispatch queue, outside of any + /// transaction. You'll get a fatal error otherwise. + /// + /// The *block* argument is guaranteed to see the database in the last + /// committed state at the moment this method is called. Eventual concurrent + /// database updates are *not visible* inside the block. + /// + /// In the example below, the number of players is fetched concurrently with + /// the player insertion. Yet the future is guaranteed to return zero: + /// + /// try writer.asyncWriteWithoutTransaction { db in + /// // Delete all players + /// try Player.deleteAll() + /// + /// // Count players concurrently + /// writer.asyncConcurrentRead { result in + /// do { + /// let db = try result.get() + /// // Guaranteed to be zero + /// let count = try Player.fetchCount(db) + /// } catch { + /// // Handle error + /// } + /// } + /// + /// // Insert a player + /// try Player(...).insert(db) + /// } + /// + /// - parameter block: A block that accesses the database. + /// :nodoc: + func spawnConcurrentRead(_ block: @escaping (Result) -> Void) + #endif } extension DatabaseWriter { @@ -549,6 +587,13 @@ public final class AnyDatabaseWriter : DatabaseWriter { return base.concurrentRead(block) } + #if compiler(>=5.0) + /// :nodoc: + public func spawnConcurrentRead(_ block: @escaping (Result) -> Void) { + base.spawnConcurrentRead(block) + } + #endif + // MARK: - Writing in Database /// :nodoc: