From 83c0e6432b84e7879a1bd4dc5bc574627936bd07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 11 Feb 2024 18:32:33 +0100 Subject: [PATCH] [BREAKING] Remove AsyncValueObservation does not need any scheduler --- .../Extension/ValueObservation.md | 2 +- GRDB/ValueObservation/ValueObservation.swift | 10 +-- TODO.md | 1 + Tests/GRDBTests/ValueObservationTests.swift | 69 ------------------- 4 files changed, 8 insertions(+), 74 deletions(-) diff --git a/GRDB/Documentation.docc/Extension/ValueObservation.md b/GRDB/Documentation.docc/Extension/ValueObservation.md index 2fbf4f9e1..335ab1aad 100644 --- a/GRDB/Documentation.docc/Extension/ValueObservation.md +++ b/GRDB/Documentation.docc/Extension/ValueObservation.md @@ -291,7 +291,7 @@ When needed, you can help GRDB optimize observations and reduce database content - ``publisher(in:scheduling:)`` - ``start(in:scheduling:onError:onChange:)`` -- ``values(in:scheduling:bufferingPolicy:)`` +- ``values(in:bufferingPolicy:)`` - ``DatabaseCancellable`` - ``ValueObservationScheduler`` diff --git a/GRDB/ValueObservation/ValueObservation.swift b/GRDB/ValueObservation/ValueObservation.swift index 37c313b20..cae810701 100644 --- a/GRDB/ValueObservation/ValueObservation.swift +++ b/GRDB/ValueObservation/ValueObservation.swift @@ -294,18 +294,20 @@ extension ValueObservation { /// ``` /// /// - parameter reader: A DatabaseReader. - /// - parameter scheduler: A ValueObservationScheduler. By default, fresh - /// values are dispatched asynchronously on the main dispatch queue. @available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) public func values( in reader: some DatabaseReader, - scheduling scheduler: some ValueObservationScheduler = .async(onQueue: .main), bufferingPolicy: AsyncValueObservation.BufferingPolicy = .unbounded) -> AsyncValueObservation where Reducer: ValueReducer { AsyncValueObservation(bufferingPolicy: bufferingPolicy) { onError, onChange in - self.start(in: reader, scheduling: scheduler, onError: onError, onChange: onChange) + self.start( + in: reader, + // TODO: can we avoid this hop to the main queue? + scheduling: .async(onQueue: .main), + onError: onError, + onChange: onChange) } } } diff --git a/TODO.md b/TODO.md index e8bb1bacf..dc7af5242 100644 --- a/TODO.md +++ b/TODO.md @@ -90,6 +90,7 @@ - AsyncValueObservation - [ ] GRDB7 JSONB https://hachyderm.io/@jason_abbott/111908186742242971#. - [ ] GRDB7: stop exporting CSQLite https://github.com/groue/GRDB.swift/issues/642#issuecomment-1937347696 +- [ ] GRDB7: AsyncValueObservation: experiment with an immediate scheduler that does not hop to the main queue. ## Unsure if necessary diff --git a/Tests/GRDBTests/ValueObservationTests.swift b/Tests/GRDBTests/ValueObservationTests.swift index 2c5571541..44a53562f 100644 --- a/Tests/GRDBTests/ValueObservationTests.swift +++ b/Tests/GRDBTests/ValueObservationTests.swift @@ -907,40 +907,6 @@ class ValueObservationTests: GRDBTestCase { try await AsyncTest(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } - @available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) - func testAsyncAwait_values_prefix_immediate_scheduling() async throws { - func test(_ writer: some DatabaseWriter) async throws { - // We need something to change - try await writer.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } - - let cancellationExpectation = expectation(description: "cancelled") - let task = Task { @MainActor () -> [Int] in - var counts: [Int] = [] - let observation = ValueObservation - .trackingConstantRegion(Table("t").fetchCount) - .handleEvents(didCancel: { cancellationExpectation.fulfill() }) - - for try await count in try observation.values(in: writer, scheduling: .immediate).prefix(while: { $0 <= 3 }) { - counts.append(count) - try await writer.write { try $0.execute(sql: "INSERT INTO t DEFAULT VALUES") } - } - return counts - } - - let counts = try await task.value - XCTAssertTrue(counts.contains(0)) - XCTAssertTrue(counts.contains(where: { $0 >= 2 })) - XCTAssertEqual(counts.sorted(), counts) - - // Observation was ended - await fulfillment(of: [cancellationExpectation], timeout: 2) - } - - try await AsyncTest(test).run { try DatabaseQueue() } - try await AsyncTest(test).runAtTemporaryDatabasePath { try DatabaseQueue(path: $0) } - try await AsyncTest(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } - } - @available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) func testAsyncAwait_values_break() async throws { func test(_ writer: some DatabaseWriter) async throws { @@ -979,41 +945,6 @@ class ValueObservationTests: GRDBTestCase { try await AsyncTest(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } - @available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) - func testAsyncAwait_values_immediate_break() async throws { - func test(_ writer: some DatabaseWriter) async throws { - // We need something to change - try await writer.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } - - let cancellationExpectation = expectation(description: "cancelled") - - let task = Task { @MainActor () -> [Int] in - var counts: [Int] = [] - let observation = ValueObservation - .trackingConstantRegion(Table("t").fetchCount) - .handleEvents(didCancel: { cancellationExpectation.fulfill() }) - - for try await count in observation.values(in: writer, scheduling: .immediate) { - counts.append(count) - break - } - return counts - } - - let counts = try await task.value - - // A single value was published - assertValueObservationRecordingMatch(recorded: counts, expected: [0]) - - // Observation was ended - await fulfillment(of: [cancellationExpectation], timeout: 2) - } - - try await AsyncTest(test).run { try DatabaseQueue() } - try await AsyncTest(test).runAtTemporaryDatabasePath { try DatabaseQueue(path: $0) } - try await AsyncTest(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } - } - @available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) func testAsyncAwait_values_cancelled() async throws { func test(_ writer: some DatabaseWriter) async throws {