From 5e89d08fd095ecaa297eb86b5b9d32bacbb86557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 6 Nov 2021 12:27:12 +0100 Subject: [PATCH] Layout Concurrency Guide --- Documentation/Concurrency.md | 46 ++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/Documentation/Concurrency.md b/Documentation/Concurrency.md index 08efb147b..6c416a1c8 100644 --- a/Documentation/Concurrency.md +++ b/Documentation/Concurrency.md @@ -122,14 +122,14 @@ let newPlayerCount = try dbQueue.write { db -> Int in } ``` -> :point_up: **Note**: It is a programmer error to perform a sync access from any other database access (this restriction can be lifted: see [Safe and Unsafe Database Accesses]): -> -> ```swift -> try dbQueue.write { db in -> // Fatal Error: Database methods are not reentrant. -> try dbQueue.write { db in ... } -> } -> ``` +It is a programmer error to perform a sync access from any other database access (this restriction can be lifted: see [Safe and Unsafe Database Accesses]): + +```swift +try dbQueue.write { db in + // Fatal Error: Database methods are not reentrant. + try dbQueue.write { db in ... } +} +``` :twisted_rightwards_arrows: **An async access does not block the current thread.** Instead, it notifies you when the database operations are completed. There are three ways to access the database asynchronously: @@ -198,21 +198,21 @@ dbQueue.asyncWrite({ (db: Database) -> Int in -> :point_up: **Note**: During one async access, all individual database operations grouped inside (fetch, insert, etc.) are synchronous: -> -> ```swift -> // When you perform ONE async access... -> dbQueue.writePublisher { db in -> // ... ALL database operations are performed synchronously: -> try Player(...).insert(db) -> try Player(...).insert(db) -> let players = try Player.fetchAll(db) -> } -> ``` -> -> This is true for all async techniques. -> -> This prevents the database operations from various concurrent accesses from being interleaved, with disastrous consequences. For example, one access must not be able to issue a `COMMIT` statement in the middle of an unfinished concurrent write! +During one async access, all individual database operations grouped inside (fetch, insert, etc.) are synchronous: + +```swift +// One asynchronous access... +dbQueue.writePublisher { db in + // ... always performs synchronous database operations: + try Player(...).insert(db) + try Player(...).insert(db) + let players = try Player.fetchAll(db) +} +``` + +This is true for all async techniques. + +This prevents the database operations from various concurrent accesses from being interleaved, with disastrous consequences. For example, one access must not be able to issue a `COMMIT` statement in the middle of an unfinished concurrent write! ## Safe and Unsafe Database Accesses