mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
DatabaseWriter.spawnConcurrentRead (experimental method)
This commit is contained in:
1 parent
cdccac8c86
commit
dc2871d5eb
3 files changed
+78
-8
No files matched your search
@@ -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<Database, Error>) -> 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).
|
||||
|
||||
@@ -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<Database, Error>) -> 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,
|
||||
|
||||
@@ -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<T>(_ block: @escaping (Database) throws -> T) -> DatabaseFuture<T>
|
||||
|
||||
#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<Database, Error>) -> 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<Database, Error>) -> Void) {
|
||||
base.spawnConcurrentRead(block)
|
||||
}
|
||||
#endif
|
||||
|
||||
// MARK: - Writing in Database
|
||||
|
||||
/// :nodoc:
|
||||
|
||||
Reference in new issue
Block a user