From 173c8af3efd4e87ae627f2755ba9fe09fc54c229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 17 Jun 2019 19:48:25 +0200 Subject: [PATCH] asyncConcurrentRead documentation --- GRDB/Core/DatabasePool.swift | 17 ++++++++-------- GRDB/Core/DatabaseQueue.swift | 4 ++-- README.md | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index cd0e5f671..25328f295 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -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) -> Void) { // Check that we're on the writer queue... diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 3ce475757..964bfbbde 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -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. diff --git a/README.md b/README.md index b95b3a26d..96a103574 100644 --- a/README.md +++ b/README.md @@ -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) 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