mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
asyncConcurrentRead documentation
This commit is contained in:
1 parent
3cea3db2b4
commit
173c8af3ef
3 files changed
+49
-10
No files matched your search
@@ -298,7 +298,7 @@ extension DatabasePool : DatabaseReader {
|
||||
/// }
|
||||
///
|
||||
/// Starting SQLite 3.8.0 (iOS 8.2+, OSX 10.10+, custom SQLite builds and
|
||||
/// SQLCipher), attempts to write in the database from this meethod throw a
|
||||
/// SQLCipher), attempts to write in the database from this method throw a
|
||||
/// DatabaseError of resultCode `SQLITE_READONLY`.
|
||||
///
|
||||
/// - parameter block: A block that accesses the database.
|
||||
@@ -505,19 +505,20 @@ extension DatabasePool : DatabaseReader {
|
||||
/// try Player.deleteAll()
|
||||
///
|
||||
/// // Count players concurrently
|
||||
/// writer.asyncConcurrentRead { db in
|
||||
/// // Guaranteed to be zero
|
||||
/// let count = try Player.fetchCount()
|
||||
/// 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)
|
||||
/// }
|
||||
///
|
||||
/// Starting SQLite 3.8.0 (iOS 8.2+, OSX 10.10+, custom SQLite builds and
|
||||
/// SQLCipher), attempts to write in the database from this meethod throw a
|
||||
/// DatabaseError of resultCode `SQLITE_READONLY`.
|
||||
///
|
||||
/// - parameter block: A block that accesses the database.
|
||||
public func asyncConcurrentRead(_ block: @escaping (Result<Database, Error>) -> Void) {
|
||||
// Check that we're on the writer queue...
|
||||
|
||||
@@ -158,7 +158,7 @@ extension DatabaseQueue {
|
||||
/// This method is *not* reentrant.
|
||||
///
|
||||
/// Starting SQLite 3.8.0 (iOS 8.2+, OSX 10.10+, custom SQLite builds and
|
||||
/// SQLCipher), attempts to write in the database from this meethod throw a
|
||||
/// SQLCipher), attempts to write in the database from this method throw a
|
||||
/// DatabaseError of resultCode `SQLITE_READONLY`.
|
||||
///
|
||||
/// - parameter block: A block that accesses the database.
|
||||
@@ -182,7 +182,7 @@ extension DatabaseQueue {
|
||||
/// }
|
||||
///
|
||||
/// Starting SQLite 3.8.0 (iOS 8.2+, OSX 10.10+, custom SQLite builds and
|
||||
/// SQLCipher), attempts to write in the database from this meethod throw a
|
||||
/// SQLCipher), attempts to write in the database from this method throw a
|
||||
/// DatabaseError of resultCode `SQLITE_READONLY`.
|
||||
///
|
||||
/// - parameter block: A block that accesses the database.
|
||||
|
||||
@@ -8464,6 +8464,44 @@ let component = MyReadOnlyComponent(reader: dbQueue)
|
||||
|
||||
The scheduled asynchronous updates have to wait for any eventual concurrent database write to complete before they can start.
|
||||
|
||||
- **`asyncConcurrentRead`**
|
||||
|
||||
The `asyncConcurrentRead` method is available on database pools only. It is the asynchronous equivalent of the `concurrentRead` described in the [Advanced DatabasePool](#advanced-databasepool) chapter.
|
||||
|
||||
It must be called from a writing dispatch queue, outside of any transaction. You'll get a fatal error otherwise.
|
||||
|
||||
The closure 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.
|
||||
|
||||
`asyncConcurrentRead` blocks until it can guarantee its closure argument an isolated access to the last committed state of the database. It then asynchronously executes the closure.
|
||||
|
||||
In the example below, the number of players is fetched concurrently with the player insertion. Yet the future is guaranteed to return zero:
|
||||
|
||||
```swift
|
||||
try writer.asyncWriteWithoutTransaction { db in
|
||||
do {
|
||||
// Delete all players
|
||||
try Player.deleteAll()
|
||||
|
||||
// <- not in a transaction here
|
||||
// Count players concurrently
|
||||
writer.asyncConcurrentRead { (result: Result<Database>) in
|
||||
try {
|
||||
let db = try result.get()
|
||||
// Guaranteed to be zero
|
||||
let count = try Player.fetchCount()
|
||||
} catch {
|
||||
// handle error
|
||||
}
|
||||
}
|
||||
|
||||
// Insert a player
|
||||
try Player(...).insert(db)
|
||||
} catch {
|
||||
// handle error
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Unsafe Concurrency APIs
|
||||
|
||||
|
||||
Reference in new issue
Block a user