mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Swift Concurrency Guide WIP
This commit is contained in:
1 parent
48cb4cfb16
commit
7f1a3fd813
2 files changed
+53
-4
No files matched your search
@@ -84,9 +84,7 @@ try dbQueue.write { db in
|
||||
|
||||
🔀 **An async access does not block the current thread.** Instead, it notifies you when the database operations are completed. There are four ways to access the database asynchronously:
|
||||
|
||||
- **Swift concurrency** (async/await)
|
||||
|
||||
[**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features)
|
||||
- **Swift concurrency** (async/await)
|
||||
|
||||
```swift
|
||||
let playerCount = try await dbQueue.read { db in
|
||||
@@ -104,6 +102,8 @@ try dbQueue.write { db in
|
||||
Note the identical method names: `read`, `write`. The async version is only available in async Swift functions.
|
||||
|
||||
The async database access methods honor task cancellation. Once an async Task is cancelled, reads and writes throw `CancellationError`, and any transaction is rollbacked.
|
||||
|
||||
See <doc:SwiftConcurrency> for more information about GRDB and Swift 6.
|
||||
|
||||
- **Combine publishers**
|
||||
|
||||
@@ -318,8 +318,9 @@ Types that conform to ``TransactionObserver`` can also use those methods in thei
|
||||
- ``DatabaseReader``
|
||||
- ``DatabaseSnapshotReader``
|
||||
|
||||
### Advanced Concurrency
|
||||
### Going Further
|
||||
|
||||
- <doc:SwiftConcurrency>
|
||||
- <doc:DatabaseSharing>
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
# Swift Concurrency
|
||||
|
||||
How to best integrate GRDB and Swift Concurrency
|
||||
|
||||
## Overview
|
||||
|
||||
GRDB leverages the strengths of both SQLite and Swift 6 when it comes to concurrency.
|
||||
|
||||
For instance, when an application connects to the database using a ``DatabasePool``, it can read from the database and display values on the screen, even while a background task is writing the results of a network request to disk. This is possible because SQLite supports [Write-Ahead Logging].
|
||||
|
||||
On the other hand, application previews and tests may prefer to connect to an in-memory ``DatabaseQueue``, which does not support concurrent access, but avoids writing to disk.
|
||||
|
||||
To choose the appropriate connection for different situations, applications can use the shared ``DatabaseWriter`` protocol, which ensures the concurrency guarantees described in <doc:Concurrency>. For examples of this integration, see the [demo apps].
|
||||
|
||||
Finally, GRDB connections support synchronous database access for applications that prefer not to display a loading screen before showing data on the screen.
|
||||
|
||||
Below, you can see the three main types of database access: reads and writes (synchronous or asynchronous), and database observation:
|
||||
|
||||
```swift
|
||||
// Read (synchronous)
|
||||
let player = try writer.read { db in
|
||||
try Player.find(db, id: 42)
|
||||
}
|
||||
|
||||
// Write (asynchronous)
|
||||
let newPlayerCount = try await writer.write { db in
|
||||
try Player(name: "Arthur").insert(db)
|
||||
return try Player.fetchCount(db)
|
||||
}
|
||||
|
||||
// Database observation
|
||||
let observation = ValueObservation.tracking { db in
|
||||
try Player.fetchAll(db)
|
||||
}
|
||||
for try await players in observation.values(in: writer) {
|
||||
print("Fresh players", players)
|
||||
}
|
||||
```
|
||||
|
||||
Database accesses are handled through closures (as shown with the db argument above). GRDB schedules them optimally, in the best interest of the application.
|
||||
|
||||
The Swift 6 compiler ensures that no data races occur during these database accesses.
|
||||
|
||||
### Section header
|
||||
|
||||
|
||||
[Write-Ahead Logging]: https://www.sqlite.org/draft/wal.html
|
||||
[demo apps]: https://github.com/groue/GRDB.swift/tree/master/Documentation/DemoApps
|
||||
Reference in new issue
Block a user