mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
[BREAKING] Remove AsyncValueObservation does not need any scheduler
This commit is contained in:
1 parent
32f41472b6
commit
83c0e6432b
4 files changed
+8
-74
No files matched your search
@@ -291,7 +291,7 @@ When needed, you can help GRDB optimize observations and reduce database content
|
|||||||
|
|
||||||
- ``publisher(in:scheduling:)``
|
- ``publisher(in:scheduling:)``
|
||||||
- ``start(in:scheduling:onError:onChange:)``
|
- ``start(in:scheduling:onError:onChange:)``
|
||||||
- ``values(in:scheduling:bufferingPolicy:)``
|
- ``values(in:bufferingPolicy:)``
|
||||||
- ``DatabaseCancellable``
|
- ``DatabaseCancellable``
|
||||||
- ``ValueObservationScheduler``
|
- ``ValueObservationScheduler``
|
||||||
|
|
||||||
|
|||||||
@@ -294,18 +294,20 @@ extension ValueObservation {
|
|||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// - parameter reader: A DatabaseReader.
|
/// - 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, *)
|
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
|
||||||
public func values(
|
public func values(
|
||||||
in reader: some DatabaseReader,
|
in reader: some DatabaseReader,
|
||||||
scheduling scheduler: some ValueObservationScheduler = .async(onQueue: .main),
|
|
||||||
bufferingPolicy: AsyncValueObservation<Reducer.Value>.BufferingPolicy = .unbounded)
|
bufferingPolicy: AsyncValueObservation<Reducer.Value>.BufferingPolicy = .unbounded)
|
||||||
-> AsyncValueObservation<Reducer.Value>
|
-> AsyncValueObservation<Reducer.Value>
|
||||||
where Reducer: ValueReducer
|
where Reducer: ValueReducer
|
||||||
{
|
{
|
||||||
AsyncValueObservation(bufferingPolicy: bufferingPolicy) { onError, onChange in
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,6 +90,7 @@
|
|||||||
- AsyncValueObservation
|
- AsyncValueObservation
|
||||||
- [ ] GRDB7 JSONB https://hachyderm.io/@jason_abbott/111908186742242971#.
|
- [ ] GRDB7 JSONB https://hachyderm.io/@jason_abbott/111908186742242971#.
|
||||||
- [ ] GRDB7: stop exporting CSQLite https://github.com/groue/GRDB.swift/issues/642#issuecomment-1937347696
|
- [ ] 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
|
## Unsure if necessary
|
||||||
|
|||||||
@@ -907,40 +907,6 @@ class ValueObservationTests: GRDBTestCase {
|
|||||||
try await AsyncTest(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) }
|
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, *)
|
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
|
||||||
func testAsyncAwait_values_break() async throws {
|
func testAsyncAwait_values_break() async throws {
|
||||||
func test(_ writer: some DatabaseWriter) 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) }
|
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, *)
|
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
|
||||||
func testAsyncAwait_values_cancelled() async throws {
|
func testAsyncAwait_values_cancelled() async throws {
|
||||||
func test(_ writer: some DatabaseWriter) async throws {
|
func test(_ writer: some DatabaseWriter) async throws {
|
||||||
|
|||||||
Reference in new issue
Block a user