From 7f1a3fd8136e983479c8bdf05b7a65eb94a867a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 22 Sep 2024 12:52:02 +0200 Subject: [PATCH] Swift Concurrency Guide WIP --- GRDB/Documentation.docc/Concurrency.md | 9 ++-- GRDB/Documentation.docc/SwiftConcurrency.md | 48 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 GRDB/Documentation.docc/SwiftConcurrency.md diff --git a/GRDB/Documentation.docc/Concurrency.md b/GRDB/Documentation.docc/Concurrency.md index 835199fba..e3424291b 100644 --- a/GRDB/Documentation.docc/Concurrency.md +++ b/GRDB/Documentation.docc/Concurrency.md @@ -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 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 +- - diff --git a/GRDB/Documentation.docc/SwiftConcurrency.md b/GRDB/Documentation.docc/SwiftConcurrency.md new file mode 100644 index 000000000..684d66af5 --- /dev/null +++ b/GRDB/Documentation.docc/SwiftConcurrency.md @@ -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 . 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