Skip to content

Commit 5b29537

Browse files
committed
Fix race condition when subscribing to SQLite Publisher
1 parent 6c377ed commit 5b29537

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Sources/SQLite/Publisher.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ struct Publisher: Combine.Publisher {
2525

2626
do {
2727
let subscription = Subscription(subscriber: AnySubscriber(subscriber))
28-
try subscription.observe(_sql, arguments: _arguments, queue: _queue, on: database)
2928
subscriber.receive(subscription: subscription)
29+
try subscription.observe(_sql, arguments: _arguments, queue: _queue, on: database)
3030
} catch {
3131
subscriber.receive(completion: .failure(error))
3232
}

Tests/SQLiteTests/PublisherTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ class PublisherTests: XCTestCase {
5454
}, after: 0.05, thenWait: 0.1)
5555
}
5656

57+
func testReceivesCurrentValuesWhenSubscribing() throws {
58+
let expectation = self.expectation(description: "Received current values")
59+
60+
let expected: Array<Array<SQLiteRow>> = [[_person1.asArguments, _person2.asArguments]]
61+
let publisher: AnyPublisher<Array<SQLiteRow>, Swift.Error> = database.publisher(Person.getAll)
62+
let sink = self.sink(for: publisher, expecting: expected, expectation: expectation)
63+
waitForExpectations(timeout: 0.5)
64+
sink.cancel()
65+
}
66+
5767
func testDeleteAsSQLiteRow() throws {
5868
let expectation = self.expectation(description: "Received two notifications")
5969

@@ -143,6 +153,7 @@ class PublisherTests: XCTestCase {
143153
static var allTests = [
144154
("testReceivesCompletionWithErrorGivenInvalidSQL", testReceivesCompletionWithErrorGivenInvalidSQL),
145155
("testCancellingForeverCancelsSubscriptions", testCancellingForeverCancelsSubscriptions),
156+
("testReceivesCurrentValuesWhenSubscribing", testReceivesCurrentValuesWhenSubscribing),
146157
("testDeleteAsSQLiteRow", testDeleteAsSQLiteRow),
147158
("testDelete", testDelete),
148159
("testDeleteFirstWhere", testDeleteFirstWhere),

0 commit comments

Comments
 (0)