From 827dc3da8b761fadfda5923cb39b486f9e7c8d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 8 Jul 2016 18:02:06 +0200 Subject: [PATCH] Cleanup --- GRDB/Core/DatabaseScheduler.swift | 17 ++++++++++++----- GRDB/Core/SerializedDatabase.swift | 14 ++++---------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/GRDB/Core/DatabaseScheduler.swift b/GRDB/Core/DatabaseScheduler.swift index f01c042c8..85391c6e3 100644 --- a/GRDB/Core/DatabaseScheduler.swift +++ b/GRDB/Core/DatabaseScheduler.swift @@ -7,22 +7,21 @@ /// https://github.com/groue/GRDB.swift/issues/55. To support this use case, a /// single dispatch queue can be temporarily shared by two or more connections. /// -/// Two entry points: -/// /// - DatabaseScheduler.makeSerializedQueueAllowing(database:) creates a /// dispatch queue that allows one database. /// /// It does so by registering one instance of DatabaseScheduler as a specific /// of the dispatch queue, a DatabaseScheduler that allows that database only. /// -/// Later on, the queue can be shared by several databases by mutating the -/// allowedDatabases property. See SerializedDatabase.sync() +/// Later on, the queue can be shared by several databases with the method +/// allowing(databases:execute:). See SerializedDatabase.sync() for +/// an example. /// /// - preconditionValidQueue() crashes whenever a database is used in an invalid /// dispatch queue. final class DatabaseScheduler { private static let specificKey = DispatchSpecificKey() - var allowedDatabases: [Database] + private(set) var allowedDatabases: [Database] private init(allowedDatabase database: Database) { allowedDatabases = [database] @@ -35,6 +34,14 @@ final class DatabaseScheduler { return queue } + // Temporarily allows `databases` while executing `body` + func allowing(databases: [Database], execute body: @noescape () throws -> T) rethrows -> T { + let backup = allowedDatabases + allowedDatabases.append(contentsOf: databases) + defer { allowedDatabases = backup } + return try body() + } + static func preconditionValidQueue(_ db: Database, _ message: @autoclosure() -> String = "Database was not used on the correct thread.", file: StaticString = #file, line: UInt = #line) { GRDBPrecondition(allows(db), message, file: file, line: line) } diff --git a/GRDB/Core/SerializedDatabase.swift b/GRDB/Core/SerializedDatabase.swift index e55a3e22e..386b2e6c7 100644 --- a/GRDB/Core/SerializedDatabase.swift +++ b/GRDB/Core/SerializedDatabase.swift @@ -73,7 +73,7 @@ final class SerializedDatabase { // } // } - if let sourceScheduler = DatabaseScheduler.current { + if let scheduler = DatabaseScheduler.current { // Case 2 or 3: // // 2. A database is invoked in a reentrant way: @@ -91,7 +91,7 @@ final class SerializedDatabase { // } // // 2 is forbidden. - GRDBPrecondition(!sourceScheduler.allows(db), "Database methods are not reentrant.") + GRDBPrecondition(!scheduler.allows(db), "Database methods are not reentrant.") // Case 3: // @@ -105,15 +105,9 @@ final class SerializedDatabase { // Let's enter the new queue, and temporarily allow the // currently allowed databases inside. return try queue.sync { - let targetScheduler = DatabaseScheduler.current! - assert(targetScheduler.allowedDatabases[0] === db) // sanity check - - let backup = targetScheduler.allowedDatabases - targetScheduler.allowedDatabases.append(contentsOf: sourceScheduler.allowedDatabases) - defer { - targetScheduler.allowedDatabases = backup + try DatabaseScheduler.current!.allowing(databases: scheduler.allowedDatabases) { + try block(db: db) } - return try block(db: db) } } else { // Case 1: