mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Confiiguration.qos and targetQueue
This commit is contained in:
1 parent
6a10ad0e11
commit
736db6a546
6 files changed
+91
-14
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import Foundation
|
||||
import Dispatch
|
||||
#if SWIFT_PACKAGE
|
||||
import CSQLite
|
||||
#elseif !GRDBCUSTOMSQLITE && !GRDBCIPHER
|
||||
@@ -136,11 +137,28 @@ public struct Configuration {
|
||||
/// Default: 5
|
||||
public var maximumReaderCount: Int = 5
|
||||
|
||||
/// The quality of service class for the work performed by the database.
|
||||
///
|
||||
/// The quality of service is ignored if you supply a target queue.
|
||||
///
|
||||
/// Default: .default (.unspecified on macOS < 10.10)
|
||||
public var qos: DispatchQoS
|
||||
|
||||
/// The target queue for the work performed by the database.
|
||||
///
|
||||
/// Default: nil
|
||||
public var targetQueue: DispatchQueue? = nil
|
||||
|
||||
// MARK: - Factory Configuration
|
||||
|
||||
/// Creates a factory configuration
|
||||
public init() { }
|
||||
public init() {
|
||||
if #available(OSX 10.10, *) {
|
||||
qos = .default
|
||||
} else {
|
||||
qos = .unspecified
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Not Public
|
||||
|
||||
@@ -243,6 +243,7 @@ extension DatabaseWriter {
|
||||
let valueObserver = try ValueObserver(
|
||||
region: observation.observedRegion(db),
|
||||
reducer: reducer,
|
||||
reducerQoS: db.configuration.qos,
|
||||
fetch: observation.fetchAfterChange(in: self),
|
||||
notificationQueue: observation.notificationQueue,
|
||||
onError: onError,
|
||||
@@ -311,6 +312,7 @@ extension ValueObservation where Reducer: ValueReducer {
|
||||
init(
|
||||
region: DatabaseRegion,
|
||||
reducer: Reducer,
|
||||
reducerQoS: DispatchQoS,
|
||||
fetch: @escaping (Database, Reducer) -> Future<Reducer.Fetched>,
|
||||
notificationQueue: DispatchQueue?,
|
||||
onError: ((Error) -> Void)?,
|
||||
@@ -322,16 +324,7 @@ extension ValueObservation where Reducer: ValueReducer {
|
||||
self.notificationQueue = notificationQueue
|
||||
self.onChange = onChange
|
||||
self.onError = onError
|
||||
if #available(OSX 10.10, *) {
|
||||
if let queue = notificationQueue {
|
||||
self.reduceQueue = DispatchQueue(label: "GRDB.ValueObservation", qos: queue.qos)
|
||||
} else {
|
||||
// Assume UI purpose
|
||||
self.reduceQueue = DispatchQueue(label: "GRDB.ValueObservation", qos: .userInitiated)
|
||||
}
|
||||
} else {
|
||||
self.reduceQueue = DispatchQueue(label: "GRDB.ValueObservation")
|
||||
}
|
||||
self.reduceQueue = DispatchQueue(label: "GRDB.ValueObservation.Reducer", qos: reducerQoS)
|
||||
}
|
||||
|
||||
func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool {
|
||||
|
||||
@@ -30,8 +30,19 @@ final class SchedulingWatchdog {
|
||||
allowedDatabases = [database]
|
||||
}
|
||||
|
||||
static func makeSerializedQueue(allowingDatabase database: Database, label: String) -> DispatchQueue {
|
||||
let queue = DispatchQueue(label: label)
|
||||
static func makeSerializedQueue(
|
||||
allowingDatabase database: Database,
|
||||
label: String,
|
||||
qos: DispatchQoS,
|
||||
targetQueue: DispatchQueue?)
|
||||
-> DispatchQueue
|
||||
{
|
||||
let queue: DispatchQueue
|
||||
if let targetQueue = targetQueue {
|
||||
queue = DispatchQueue(label: label, target: targetQueue)
|
||||
} else {
|
||||
queue = DispatchQueue(label: label, qos: qos)
|
||||
}
|
||||
let watchdog = SchedulingWatchdog(allowedDatabase: database)
|
||||
queue.setSpecific(key: specificKey, value: watchdog)
|
||||
return queue
|
||||
|
||||
@@ -38,7 +38,11 @@ final class SerializedDatabase {
|
||||
config.threadingMode = .multiThread
|
||||
|
||||
db = try Database(path: path, configuration: config, schemaCache: schemaCache)
|
||||
queue = SchedulingWatchdog.makeSerializedQueue(allowingDatabase: db, label: label)
|
||||
queue = SchedulingWatchdog.makeSerializedQueue(
|
||||
allowingDatabase: db,
|
||||
label: label,
|
||||
qos: configuration.qos,
|
||||
targetQueue: configuration.targetQueue)
|
||||
self.path = path
|
||||
try queue.sync {
|
||||
try db.setup()
|
||||
|
||||
@@ -1097,4 +1097,29 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
|
||||
@available(OSX 10.12, *)
|
||||
func testTargetQueue() throws {
|
||||
func test(targetQueue: DispatchQueue) throws {
|
||||
dbConfiguration.targetQueue = targetQueue
|
||||
let dbPool = try makeDatabasePool()
|
||||
try dbPool.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
try dbPool.read { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
}
|
||||
|
||||
// background queue
|
||||
try test(targetQueue: .global(qos: .background))
|
||||
|
||||
// main queue
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
try! test(targetQueue: .main)
|
||||
expectation.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import Dispatch
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
@available(OSX 10.12, *)
|
||||
class DatabaseQueueTests: GRDBTestCase {
|
||||
|
||||
// Until SPM tests can load resources, disable this test for SPM.
|
||||
@@ -131,4 +132,29 @@ class DatabaseQueueTests: GRDBTestCase {
|
||||
XCTAssertEqual(label, "Toreador")
|
||||
}
|
||||
}
|
||||
|
||||
@available(OSX 10.12, *)
|
||||
func testTargetQueue() throws {
|
||||
func test(targetQueue: DispatchQueue) throws {
|
||||
dbConfiguration.targetQueue = targetQueue
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
dbQueue.read { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
}
|
||||
|
||||
// background queue
|
||||
try test(targetQueue: .global(qos: .background))
|
||||
|
||||
// main queue
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
try! test(targetQueue: .main)
|
||||
expectation.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user