Skip to content

Commit 8b58ec4

Browse files
committed
Merge branch 'dev/issue1194' into development
2 parents fafa74c + 9b7b603 commit 8b58ec4

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

GRDB/Core/Configuration.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,17 @@ public struct Configuration {
316316
return DispatchQueue(label: label, qos: qos)
317317
}
318318
}
319+
320+
/// Creates a DispatchQueue which has the quality of service of
321+
/// read accesses.
322+
///
323+
/// The returned queue has no target queue, and won't create deadlocks when
324+
/// used synchronously from a database access.
325+
func makeDispatchQueue(label: String) -> DispatchQueue {
326+
if let targetQueue = targetQueue {
327+
return DispatchQueue(label: label, qos: targetQueue.qos)
328+
} else {
329+
return DispatchQueue(label: label, qos: qos)
330+
}
331+
}
319332
}

GRDB/ValueObservation/ValueConcurrentObserver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ final class ValueConcurrentObserver<Reducer: ValueReducer> {
138138
fetch: reducer._fetch)
139139
self.notificationCallbacks = NotificationCallbacks(events: events, onChange: onChange)
140140
self.reducer = reducer
141-
self.reduceQueue = dbPool.configuration.makeReaderDispatchQueue(label: dbPool.configuration.identifier(
141+
self.reduceQueue = dbPool.configuration.makeDispatchQueue(label: dbPool.configuration.identifier(
142142
defaultLabel: "GRDB",
143143
purpose: "ValueObservation"))
144144
}

GRDB/ValueObservation/ValueWriteOnlyObserver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ final class ValueWriteOnlyObserver<Writer: DatabaseWriter, Reducer: ValueReducer
139139
fetch: reducer._fetch)
140140
self.notificationCallbacks = NotificationCallbacks(events: events, onChange: onChange)
141141
self.reducer = reducer
142-
self.reduceQueue = writer.configuration.makeReaderDispatchQueue(label: writer.configuration.identifier(
142+
self.reduceQueue = writer.configuration.makeDispatchQueue(label: writer.configuration.identifier(
143143
defaultLabel: "GRDB",
144144
purpose: "ValueObservation"))
145145
}

Tests/GRDBCombineTests/ValueObservationPublisherTests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,5 +423,53 @@ class ValueObservationPublisherTests : XCTestCase {
423423
.runAtTemporaryDatabasePath { try setUp(DatabaseQueue(path: $0)) }
424424
.runAtTemporaryDatabasePath { try setUp(DatabasePool(path: $0)) }
425425
}
426+
427+
// MARK: - Regression Tests
428+
429+
/// Regression test for https://github.com/groue/GRDB.swift/issues/1194
430+
func testIssue1194() throws {
431+
guard #available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *) else {
432+
throw XCTSkip("Combine is not available")
433+
}
434+
435+
struct Record: Codable, FetchableRecord, PersistableRecord {
436+
var id: Int64
437+
}
438+
439+
var configuration = Configuration()
440+
configuration.targetQueue = DispatchQueue(label: "crash.test", qos: .userInitiated)
441+
442+
let database = DatabaseQueue(configuration: configuration)
443+
444+
var migrator = DatabaseMigrator()
445+
migrator.registerMigration("v1") { (db) in
446+
try db.create(table: Record.databaseTableName) { (t) in
447+
t.autoIncrementedPrimaryKey("id")
448+
}
449+
}
450+
451+
try migrator.migrate(database)
452+
453+
let observation = ValueObservation.tracking { (db) in
454+
try Record.fetchCount(db)
455+
}
456+
457+
let exp = expectation(description: "")
458+
let cancellable = observation.publisher(in: database, scheduling: .immediate)
459+
.map { _ in
460+
database.readPublisher { (db) in
461+
try Record.fetchCount(db)
462+
}
463+
}
464+
.switchToLatest()
465+
.sink(receiveCompletion: { _ in },
466+
receiveValue: { (value) in
467+
exp.fulfill()
468+
})
469+
470+
withExtendedLifetime(cancellable) {
471+
waitForExpectations(timeout: 1)
472+
}
473+
}
426474
}
427475
#endif

0 commit comments

Comments
 (0)