diff --git a/Documentation/Concurrency.md b/Documentation/Concurrency.md index 0069b54c6..99c1958bd 100644 --- a/Documentation/Concurrency.md +++ b/Documentation/Concurrency.md @@ -151,28 +151,6 @@ let newPlayerCount = try await dbQueue.write { db -> Int in Note the identical method names: `read`, `write`. The async version is only available in async Swift functions. -> :point_up: **Note**: The `save` and `insert` [persistence methods](../README.md#persistablerecord-protocol) can trigger compiler errors in async contexts: -> -> ```swift -> // Error: Mutation of captured var 'player' in concurrently-executing code -> var player = Player(id: nil, name: "Arthur") -> try await dbWriter.write { db in -> try player.insert(db) -> } -> print(player.id) // A non-nil id -> ``` -> -> When this happens, you may prefer the `saved` and `inserted` methods instead: -> -> ```swift -> // OK -> var player = Player(id: nil, name: "Arthur") -> player = try await dbWriter.write { [player] db in -> return try player.inserted(db) -> } -> print(player.id) // A non-nil id -> ``` -
@@ -264,12 +242,12 @@ dbQueue.asyncWrite({ (db: Database) -> Int in - **[Serialized Writes]** - All writes performed by one [DatabaseQueue] or [DatabasePool] instance are serialized. *Why is it important?* - this guarantee prevents [SQLITE_BUSY] errors during concurrent writes. - **[Write Transactions]** - All writes are wrapped in a transaction. *Why is it important?* - concurrent reads can not see partial database updates (even reads performed by other processes). - **[Isolated Reads]** - All reads are wrapped in a transaction. *Why is it important?* - an isolated read sees a stable and immutable state of the database[^1], and does not see changes performed by eventual concurrent writes (even writes performed by other processes). -- **[Forbidden Writes]** - Inside a read database access, all attempts to write raise an error. *Why is it important?* - this enforces the immutability of the database during a read. +- **[Forbidden Writes]** - Inside a read access, all attempts to write raise an error. *Why is it important?* - this enforces the immutability of the database during a read. - **[Non-Reentrancy]** - Database accesses are not reentrant. *Why is it important?* - this reduces the opportunities for deadlocks, and fosters the clear transaction boundaries of the [second concurrency rule](#rule-2). -These guarantees are important. They allow you to rely on the invariants of your database. They allow you to reason about the components of your application independently. +These guarantees allow you to rely on the invariants of your database. They allow you to reason about the components of your application independently. -Some applications, however, need to relax this safety net in order to achieve some SQLite operations. In this case, you will replace `read` and `write` with one of the methods below: +Some applications need to relax this safety net in order to achieve some SQLite operations. In this case, replace `read` and `write` with one of the methods below: - **Write outside of any transaction** (Lifted guarantee: [Write Transactions]) @@ -340,7 +318,7 @@ Some applications, however, need to relax this safety net in order to achieve so `unsafeReentrantRead` has no async version. -:warning: By using one of the methods above, you become responsible of the thread-safety of your application. Please understand the consequences of lifting each concurrency guarantee. Some guarantees can also be restored at your convenience: +:point_up: **By using one of the methods above, you become responsible of the thread-safety of your application.** Please understand the consequences of lifting each concurrency guarantee. Some guarantees can also be restored at your convenience: - The [Write Transactions] and [Isolated Reads] guarantees can be restored at any point, by opening an [explicit transaction](../README.md#transactions-and-savepoints). For example: diff --git a/README.md b/README.md index 8fe9e2629..3550c03ea 100644 --- a/README.md +++ b/README.md @@ -7519,6 +7519,7 @@ FAQ **[FAQ: Errors](#faq-errors)** - [Generic parameter 'T' could not be inferred](#generic-parameter-t-could-not-be-inferred) +- [Mutation of captured var in concurrently-executing code](#mutation-of-captured-var-in-concurrently-executing-code) - [SQLite error 1 "no such column"](#sqlite-error-1-no-such-column) - [SQLite error 10 "disk I/O error", SQLite error 23 "not authorized"](#sqlite-error-10-disk-io-error-sqlite-error-23-not-authorized) - [SQLite error 21 "wrong number of statement arguments" with LIKE queries](#sqlite-error-21-wrong-number-of-statement-arguments-with-like-queries) @@ -7858,6 +7859,7 @@ If after all those steps (thanks you!), your observation is still failing you, p - :arrow_up: [FAQ] - [Generic parameter 'T' could not be inferred](#generic-parameter-t-could-not-be-inferred) +- [Mutation of captured var in concurrently-executing code](#mutation-of-captured-var-in-concurrently-executing-code) - [SQLite error 1 "no such column"](#sqlite-error-1-no-such-column) - [SQLite error 10 "disk I/O error", SQLite error 23 "not authorized"](#sqlite-error-10-disk-io-error-sqlite-error-23-not-authorized) - [SQLite error 21 "wrong number of statement arguments" with LIKE queries](#sqlite-error-21-wrong-number-of-statement-arguments-with-like-queries) @@ -7896,6 +7898,31 @@ let string = try dbQueue.read { db in ``` +### Mutation of captured var in concurrently-executing code + +The `insert` and `save` [persistence methods](#persistablerecord-protocol) can trigger a compiler error in async contexts: + +```swift +var player = Player(id: nil, name: "Arthur") +try await dbWriter.write { db in + // Error: Mutation of captured var 'player' in concurrently-executing code + try player.insert(db) +} +print(player.id) // A non-nil id +``` + +When this happens, prefer the `inserted` and `saved` methods instead: + +```swift +// OK +var player = Player(id: nil, name: "Arthur") +player = try await dbWriter.write { [player] db in + return try player.inserted(db) +} +print(player.id) // A non-nil id +``` + + ### SQLite error 1 "no such column" This error message is self-explanatory: do check for misspelled or non-existing column names.