Files
GRDB.swift/GRDB/Core/DatabaseScheduler.swift
T
2016-07-08 18:02:06 +02:00

61 lines
2.5 KiB
Swift

/// DatabaseScheduler makes sure that databases connections are used on correct
/// dispatch queues, and warns the user with a fatal error whenever she misuses
/// a database connection.
///
/// Generally speaking, each connection has its own dispatch queue. But it's not
/// enough: users need to use two database connections at the same time:
/// 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.
///
/// - 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 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<DatabaseScheduler>()
private(set) var allowedDatabases: [Database]
private init(allowedDatabase database: Database) {
allowedDatabases = [database]
}
static func makeSerializedQueueAllowing(database: Database) -> DispatchQueue {
let queue = DispatchQueue(label: "GRDB.SerializedDatabase")
let scheduler = DatabaseScheduler(allowedDatabase: database)
queue.setSpecific(key: specificKey, value: scheduler)
return queue
}
// Temporarily allows `databases` while executing `body`
func allowing<T>(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)
}
static func allows(_ db: Database) -> Bool {
return current?.allows(db) ?? false
}
func allows(_ db: Database) -> Bool {
return allowedDatabases.contains { $0 === db }
}
static var current: DatabaseScheduler? {
return DispatchQueue.getSpecific(key: specificKey)
}
}