mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
ValueObservation tracks a varying database region by defaut
This commit is contained in:
1 parent
2c51c04708
commit
46c3839ea0
13 files changed
+169
-84
No files matched your search
@@ -409,14 +409,85 @@ extension ValueObservation where Reducer == ValueReducers.Auto {
|
||||
|
||||
// MARK: - Creating ValueObservation
|
||||
|
||||
/// Creates a ValueObservation which notifies the values returned by the
|
||||
/// *fetch* function whenever a database transaction changes them.
|
||||
/// Creates an optimized `ValueObservation` that notifies the values
|
||||
/// returned by the `fetch` function whenever a database transaction
|
||||
/// changes them.
|
||||
///
|
||||
/// The *fetch* function must always performs the same database requests.
|
||||
/// The stability of the observed database region allows optimizations.
|
||||
/// The optimization only kicks in when the observation is started from a
|
||||
/// `DatabasePool`: fresh values are fetched concurrently, and do not block
|
||||
/// database writes.
|
||||
///
|
||||
/// When you want to observe a varying database region, use the
|
||||
/// `ValueObservation.trackingVaryingRegion(_:)` method instead.
|
||||
/// - precondition: The *fetch* function must perform requests that fetch
|
||||
/// from a single and constant database region. The tracked region is made
|
||||
/// of tables, columns, and, when possible, rowids of individual rows. All
|
||||
/// changes that happen outside of this region do not impact
|
||||
/// the observation.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// // Tracks the full 'player' table
|
||||
/// let observation = ValueObservation.trackingConstantRegion { db -> [Player] in
|
||||
/// try Player.fetchAll(db)
|
||||
/// }
|
||||
///
|
||||
/// // Tracks the row with id 42 in the 'player' table
|
||||
/// let observation = ValueObservation.trackingConstantRegion { db -> Player? in
|
||||
/// try Player.fetchOne(db, key: 42)
|
||||
/// }
|
||||
///
|
||||
/// // Tracks the 'score' column in the 'player' table
|
||||
/// let observation = ValueObservation.trackingConstantRegion { db -> Int? in
|
||||
/// try Player.select(max(Column("score"))).fetchOne(db)
|
||||
/// }
|
||||
///
|
||||
/// // Tracks both the 'player' and 'team' tables
|
||||
/// let observation = ValueObservation.trackingConstantRegion { db -> ([Team], [Player]) in
|
||||
/// let teams = try Team.fetchAll(db)
|
||||
/// let players = try Player.fetchAll(db)
|
||||
/// return (teams, players)
|
||||
/// }
|
||||
///
|
||||
/// When you want to observe a varying database region, make sure you use
|
||||
/// the `ValueObservation.tracking(_:)` method instead, or else some changes
|
||||
/// will not be notified.
|
||||
///
|
||||
/// For example, consider those three observations below that depend on some
|
||||
/// user preference. They all track a varying region, and must
|
||||
/// use `ValueObservation.tracking(_:)`:
|
||||
///
|
||||
/// // Does not always track the same row in the player table.
|
||||
/// let observation = ValueObservation.tracking { db -> Player? in
|
||||
/// let pref = try Preference.fetchOne(db) ?? .default
|
||||
/// return try Player.fetchOne(db, key: pref.favoritePlayerId)
|
||||
/// }
|
||||
///
|
||||
/// // Only tracks the 'user' table if there are some blocked emails.
|
||||
/// let observation = ValueObservation.tracking { db -> [User] in
|
||||
/// let pref = try Preference.fetchOne(db) ?? .default
|
||||
/// let blockedEmails = pref.blockedEmails
|
||||
/// return try User.filter(blockedEmails.contains(Column("email"))).fetchAll(db)
|
||||
/// }
|
||||
///
|
||||
/// // Sometimes tracks the 'food' table, and sometimes the 'beverage' table.
|
||||
/// let observation = ValueObservation.tracking { db -> Int in
|
||||
/// let pref = try Preference.fetchOne(db) ?? .default
|
||||
/// switch pref.selection {
|
||||
/// case .food: return try Food.fetchCount(db)
|
||||
/// case .beverage: return try Beverage.fetchCount(db)
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// - parameter fetch: A function that fetches the observed value from
|
||||
/// the database.
|
||||
public static func trackingConstantRegion<Value>(
|
||||
_ fetch: @escaping (Database) throws -> Value)
|
||||
-> ValueObservation<ValueReducers.Fetch<Value>>
|
||||
{
|
||||
.init(makeReducer: { .init(isSelectedRegionDeterministic: true, fetch: fetch) })
|
||||
}
|
||||
|
||||
/// Creates a `ValueObservation` that notifies the values returned by the
|
||||
/// `fetch` function whenever a database transaction changes them.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
@@ -427,7 +498,7 @@ extension ValueObservation where Reducer == ValueReducers.Auto {
|
||||
/// let cancellable = try observation.start(
|
||||
/// in: dbQueue,
|
||||
/// onError: { error in ... },
|
||||
/// onChange:) { players: [Player] in
|
||||
/// onChange: { players: [Player] in
|
||||
/// print("Players have changed")
|
||||
/// })
|
||||
///
|
||||
@@ -437,18 +508,32 @@ extension ValueObservation where Reducer == ValueReducers.Auto {
|
||||
_ fetch: @escaping (Database) throws -> Value)
|
||||
-> ValueObservation<ValueReducers.Fetch<Value>>
|
||||
{
|
||||
.init(makeReducer: { .init(isSelectedRegionDeterministic: true, fetch: fetch) })
|
||||
.init(makeReducer: { .init(isSelectedRegionDeterministic: false, fetch: fetch) })
|
||||
}
|
||||
|
||||
/// Creates a ValueObservation which notifies the values returned by the
|
||||
/// *fetch* function whenever a database transaction changes them.
|
||||
/// Creates a `ValueObservation` that notifies the values returned by the
|
||||
/// `fetch` function whenever a database transaction changes them.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// let observation = ValueObservation.tracking { db in
|
||||
/// try Player.fetchAll(db)
|
||||
/// }
|
||||
///
|
||||
/// let cancellable = try observation.start(
|
||||
/// in: dbQueue,
|
||||
/// onError: { error in ... },
|
||||
/// onChange: { players: [Player] in
|
||||
/// print("Players have changed")
|
||||
/// })
|
||||
///
|
||||
/// - parameter fetch: A function that fetches the observed value from
|
||||
/// the database.
|
||||
@available(*, deprecated, renamed: "tracking(_:)")
|
||||
public static func trackingVaryingRegion<Value>(
|
||||
_ fetch: @escaping (Database) throws -> Value)
|
||||
-> ValueObservation<ValueReducers.Fetch<Value>>
|
||||
{
|
||||
.init(makeReducer: { .init(isSelectedRegionDeterministic: false, fetch: fetch) })
|
||||
tracking(fetch)
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ class ValueObservationPublisherTests : XCTestCase {
|
||||
|
||||
func test(writer: DatabaseWriter) throws {
|
||||
let publisher = ValueObservation
|
||||
.tracking(Player.fetchCount)
|
||||
.trackingConstantRegion(Player.fetchCount)
|
||||
.publisher(in: writer)
|
||||
let recorder = publisher.record()
|
||||
|
||||
@@ -78,7 +78,7 @@ class ValueObservationPublisherTests : XCTestCase {
|
||||
let expectation = self.expectation(description: "")
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
let cancellable = ValueObservation
|
||||
.tracking(Player.fetchCount)
|
||||
.trackingConstantRegion(Player.fetchCount)
|
||||
.publisher(in: writer)
|
||||
.sink(
|
||||
receiveCompletion: { _ in },
|
||||
@@ -105,7 +105,7 @@ class ValueObservationPublisherTests : XCTestCase {
|
||||
|
||||
func test(writer: DatabaseWriter) throws {
|
||||
let publisher = ValueObservation
|
||||
.tracking { try $0.execute(sql: "THIS IS NOT SQL") }
|
||||
.trackingConstantRegion { try $0.execute(sql: "THIS IS NOT SQL") }
|
||||
.publisher(in: writer)
|
||||
let recorder = publisher.record()
|
||||
let completion = try wait(for: recorder.completion, timeout: 1)
|
||||
@@ -137,7 +137,7 @@ class ValueObservationPublisherTests : XCTestCase {
|
||||
|
||||
func test(writer: DatabaseWriter) throws {
|
||||
let publisher = ValueObservation
|
||||
.tracking(Player.fetchCount)
|
||||
.trackingConstantRegion(Player.fetchCount)
|
||||
.publisher(in: writer, scheduling: .immediate)
|
||||
let recorder = publisher.record()
|
||||
|
||||
@@ -190,7 +190,7 @@ class ValueObservationPublisherTests : XCTestCase {
|
||||
})
|
||||
|
||||
let observationCancellable = ValueObservation
|
||||
.tracking(Player.fetchCount)
|
||||
.trackingConstantRegion(Player.fetchCount)
|
||||
.publisher(in: writer, scheduling: .immediate)
|
||||
.subscribe(testSubject)
|
||||
|
||||
@@ -212,7 +212,7 @@ class ValueObservationPublisherTests : XCTestCase {
|
||||
|
||||
func test(writer: DatabaseWriter) throws {
|
||||
let publisher = ValueObservation
|
||||
.tracking { try $0.execute(sql: "THIS IS NOT SQL") }
|
||||
.trackingConstantRegion { try $0.execute(sql: "THIS IS NOT SQL") }
|
||||
.publisher(in: writer, scheduling: .immediate)
|
||||
let recorder = publisher.record()
|
||||
let completion = try recorder.completion.get()
|
||||
@@ -283,7 +283,7 @@ class ValueObservationPublisherTests : XCTestCase {
|
||||
receiveValue: { _ in expectation.fulfill() })
|
||||
|
||||
ValueObservation
|
||||
.tracking(Player.fetchCount)
|
||||
.trackingConstantRegion(Player.fetchCount)
|
||||
.publisher(in: writer)
|
||||
.subscribe(subscriber)
|
||||
|
||||
@@ -320,7 +320,7 @@ class ValueObservationPublisherTests : XCTestCase {
|
||||
})
|
||||
|
||||
ValueObservation
|
||||
.tracking(Player.fetchCount)
|
||||
.trackingConstantRegion(Player.fetchCount)
|
||||
.publisher(in: writer)
|
||||
.subscribe(subscriber)
|
||||
|
||||
@@ -359,7 +359,7 @@ class ValueObservationPublisherTests : XCTestCase {
|
||||
receiveValue: { _ in expectation.fulfill() })
|
||||
|
||||
ValueObservation
|
||||
.tracking(Player.fetchCount)
|
||||
.trackingConstantRegion(Player.fetchCount)
|
||||
.publisher(in: writer, scheduling: .immediate /* make sure we get the initial db state */)
|
||||
.subscribe(subscriber)
|
||||
|
||||
@@ -403,7 +403,7 @@ class ValueObservationPublisherTests : XCTestCase {
|
||||
})
|
||||
|
||||
ValueObservation
|
||||
.tracking(Player.fetchCount)
|
||||
.trackingConstantRegion(Player.fetchCount)
|
||||
.publisher(in: writer, scheduling: .immediate /* make sure we get two db states */)
|
||||
.subscribe(subscriber)
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ class ValueObservationCountTests: GRDBTestCase {
|
||||
struct T: TableRecord { }
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(T.fetchCount),
|
||||
ValueObservation.trackingConstantRegion(T.fetchCount),
|
||||
records: [0, 1, 1, 2, 3, 4],
|
||||
setup: { db in
|
||||
try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)")
|
||||
@@ -29,7 +29,7 @@ class ValueObservationCountTests: GRDBTestCase {
|
||||
struct T: TableRecord { }
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(T.fetchCount).removeDuplicates(),
|
||||
ValueObservation.trackingConstantRegion(T.fetchCount).removeDuplicates(),
|
||||
records: [0, 1, 2, 3, 4],
|
||||
setup: { db in
|
||||
try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)")
|
||||
|
||||
@@ -21,7 +21,7 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
|
||||
let request = SQLRequest<Name>(sql: "SELECT name FROM t ORDER BY id")
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchAll),
|
||||
ValueObservation.trackingConstantRegion(request.fetchAll),
|
||||
records: [
|
||||
[],
|
||||
[Name(rawValue: "foo")],
|
||||
@@ -44,7 +44,7 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
|
||||
})
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchAll).removeDuplicates(),
|
||||
ValueObservation.trackingConstantRegion(request.fetchAll).removeDuplicates(),
|
||||
records: [
|
||||
[],
|
||||
[Name(rawValue: "foo")],
|
||||
@@ -70,7 +70,7 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
|
||||
let request = SQLRequest<Name>(sql: "SELECT name FROM t ORDER BY id DESC")
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchOne),
|
||||
ValueObservation.trackingConstantRegion(request.fetchOne),
|
||||
records: [
|
||||
nil,
|
||||
Name(rawValue: "foo"),
|
||||
@@ -103,7 +103,7 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
|
||||
})
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchOne).removeDuplicates(),
|
||||
ValueObservation.trackingConstantRegion(request.fetchOne).removeDuplicates(),
|
||||
records: [
|
||||
nil,
|
||||
Name(rawValue: "foo"),
|
||||
@@ -137,7 +137,7 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
|
||||
let request = SQLRequest<Name?>(sql: "SELECT name FROM t ORDER BY id")
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchAll),
|
||||
ValueObservation.trackingConstantRegion(request.fetchAll),
|
||||
records: [
|
||||
[],
|
||||
[Name(rawValue: "foo")],
|
||||
@@ -160,7 +160,7 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
|
||||
})
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchAll).removeDuplicates(),
|
||||
ValueObservation.trackingConstantRegion(request.fetchAll).removeDuplicates(),
|
||||
records: [
|
||||
[],
|
||||
[Name(rawValue: "foo")],
|
||||
@@ -186,7 +186,7 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
|
||||
let request = SQLRequest<Name?>(sql: "SELECT name FROM t ORDER BY id DESC")
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchOne),
|
||||
ValueObservation.trackingConstantRegion(request.fetchOne),
|
||||
records: [
|
||||
nil,
|
||||
Name(rawValue: "foo"),
|
||||
@@ -219,7 +219,7 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
|
||||
})
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchOne).removeDuplicates(),
|
||||
ValueObservation.trackingConstantRegion(request.fetchOne).removeDuplicates(),
|
||||
records: [
|
||||
nil,
|
||||
Name(rawValue: "foo"),
|
||||
|
||||
@@ -4,7 +4,7 @@ import GRDB
|
||||
class ValueObservationFetchTests: GRDBTestCase {
|
||||
func testFetch() throws {
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking {
|
||||
ValueObservation.trackingConstantRegion {
|
||||
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
|
||||
},
|
||||
records: [0, 1, 1, 2],
|
||||
@@ -21,7 +21,7 @@ class ValueObservationFetchTests: GRDBTestCase {
|
||||
func testRemoveDuplicated() throws {
|
||||
try assertValueObservation(
|
||||
ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")! }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")! }
|
||||
.removeDuplicates(),
|
||||
records: [0, 1, 2],
|
||||
setup: { db in
|
||||
|
||||
@@ -4,7 +4,7 @@ import GRDB
|
||||
class ValueObservationMapTests: GRDBTestCase {
|
||||
func testMap() throws {
|
||||
let valueObservation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")! }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")! }
|
||||
.map { "\($0)" }
|
||||
|
||||
try assertValueObservation(
|
||||
@@ -20,7 +20,7 @@ class ValueObservationMapTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
func testMapPreservesConfiguration() {
|
||||
var observation = ValueObservation.tracking { _ in }
|
||||
var observation = ValueObservation.trackingConstantRegion { _ in }
|
||||
observation.requiresWriteAccess = true
|
||||
|
||||
let mappedObservation = observation.map { _ in }
|
||||
|
||||
@@ -31,7 +31,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
func test(_ dbReader: DatabaseReader) throws {
|
||||
let logger = TestStream()
|
||||
let observation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.print(to: logger)
|
||||
|
||||
let expectation = self.expectation(description: "")
|
||||
@@ -65,7 +65,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
func test(_ dbReader: DatabaseReader) throws {
|
||||
let logger = TestStream()
|
||||
let observation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.print(to: logger)
|
||||
|
||||
let expectation = self.expectation(description: "")
|
||||
@@ -97,7 +97,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
struct TestError: Error { }
|
||||
let logger = TestStream()
|
||||
let observation = ValueObservation
|
||||
.tracking { _ in throw TestError() }
|
||||
.trackingConstantRegion { _ in throw TestError() }
|
||||
.print(to: logger)
|
||||
|
||||
let expectation = self.expectation(description: "")
|
||||
@@ -129,7 +129,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
struct TestError: Error { }
|
||||
let logger = TestStream()
|
||||
let observation = ValueObservation
|
||||
.tracking { _ in throw TestError() }
|
||||
.trackingConstantRegion { _ in throw TestError() }
|
||||
.print(to: logger)
|
||||
|
||||
let expectation = self.expectation(description: "")
|
||||
@@ -164,7 +164,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
|
||||
let logger = TestStream()
|
||||
var observation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.print(to: logger)
|
||||
observation.requiresWriteAccess = true
|
||||
|
||||
@@ -206,7 +206,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
|
||||
let logger = TestStream()
|
||||
var observation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.print(to: logger)
|
||||
observation.requiresWriteAccess = true
|
||||
|
||||
@@ -244,7 +244,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
let logger = TestStream()
|
||||
var observation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.print(to: logger)
|
||||
observation.requiresWriteAccess = true
|
||||
|
||||
@@ -271,7 +271,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
let logger = TestStream()
|
||||
var observation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.print(to: logger)
|
||||
observation.requiresWriteAccess = true
|
||||
|
||||
@@ -302,7 +302,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
|
||||
let logger = TestStream()
|
||||
var observation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.print(to: logger)
|
||||
observation.requiresWriteAccess = true
|
||||
|
||||
@@ -345,7 +345,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
|
||||
let logger = TestStream()
|
||||
var observation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.print(to: logger)
|
||||
observation.requiresWriteAccess = true
|
||||
|
||||
@@ -394,7 +394,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
// transaction observer, some write did happen.
|
||||
var needsChange = true
|
||||
let observation = ValueObservation
|
||||
.tracking({ db -> Int? in
|
||||
.trackingConstantRegion({ db -> Int? in
|
||||
if needsChange {
|
||||
needsChange = false
|
||||
try dbPool.write { db in
|
||||
@@ -441,7 +441,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
// transaction observer, some write did happen.
|
||||
var needsChange = true
|
||||
let observation = ValueObservation
|
||||
.tracking({ db -> Int? in
|
||||
.trackingConstantRegion({ db -> Int? in
|
||||
if needsChange {
|
||||
needsChange = false
|
||||
try dbPool.write { db in
|
||||
@@ -491,7 +491,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
|
||||
let logger = TestStream()
|
||||
let observation = ValueObservation
|
||||
.trackingVaryingRegion({ db -> Int? in
|
||||
.tracking({ db -> Int? in
|
||||
let table = try String.fetchOne(db, sql: "SELECT t FROM choice")!
|
||||
return try Int.fetchOne(db, sql: "SELECT MAX(id) FROM \(table)")
|
||||
})
|
||||
@@ -543,7 +543,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
let logger1 = TestStream()
|
||||
let logger2 = TestStream()
|
||||
let observation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.print("", to: logger1)
|
||||
.print("log", to: logger2)
|
||||
|
||||
@@ -575,7 +575,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
let logger1 = TestStream()
|
||||
let logger2 = TestStream()
|
||||
let observation = ValueObservation
|
||||
.tracking { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.trackingConstantRegion { try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player") }
|
||||
.print(to: logger1)
|
||||
.removeDuplicates()
|
||||
.map { _ in "foo" }
|
||||
@@ -668,7 +668,7 @@ class ValueObservationPrintTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
let observation = ValueObservation.tracking {
|
||||
let observation = ValueObservation.trackingConstantRegion {
|
||||
try Int.fetchOne($0, sql: "SELECT MAX(id) FROM player")
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ class ValueObservationQueryInterfaceRequestTests: GRDBTestCase {
|
||||
.including(all: Parent.children.orderByPrimaryKey())
|
||||
.orderByPrimaryKey()
|
||||
.asRequest(of: Row.self)
|
||||
let observation = ValueObservation.tracking(request.fetchOne)
|
||||
let observation = ValueObservation.trackingConstantRegion(request.fetchOne)
|
||||
|
||||
let recorder = observation.record(in: dbQueue)
|
||||
try dbQueue.writeWithoutTransaction(performDatabaseModifications)
|
||||
@@ -113,7 +113,7 @@ class ValueObservationQueryInterfaceRequestTests: GRDBTestCase {
|
||||
.including(all: Parent.children.orderByPrimaryKey())
|
||||
.orderByPrimaryKey()
|
||||
.asRequest(of: Row.self)
|
||||
let observation = ValueObservation.tracking(request.fetchAll)
|
||||
let observation = ValueObservation.trackingConstantRegion(request.fetchAll)
|
||||
|
||||
let recorder = observation.record(in: dbQueue)
|
||||
try dbQueue.writeWithoutTransaction(performDatabaseModifications)
|
||||
@@ -171,7 +171,7 @@ class ValueObservationQueryInterfaceRequestTests: GRDBTestCase {
|
||||
.asRequest(of: ParentInfo.self)
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchOne),
|
||||
ValueObservation.trackingConstantRegion(request.fetchOne),
|
||||
records: [
|
||||
nil,
|
||||
ParentInfo(
|
||||
@@ -205,7 +205,7 @@ class ValueObservationQueryInterfaceRequestTests: GRDBTestCase {
|
||||
// The fundamental technique for removing duplicates of non-Equatable types
|
||||
try assertValueObservation(
|
||||
ValueObservation
|
||||
.tracking { db in try Row.fetchOne(db, request) }
|
||||
.trackingConstantRegion { db in try Row.fetchOne(db, request) }
|
||||
.removeDuplicates()
|
||||
.map { row in row.map(ParentInfo.init(row:)) },
|
||||
records: [
|
||||
@@ -240,7 +240,7 @@ class ValueObservationQueryInterfaceRequestTests: GRDBTestCase {
|
||||
.asRequest(of: ParentInfo.self)
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchAll),
|
||||
ValueObservation.trackingConstantRegion(request.fetchAll),
|
||||
records: [
|
||||
[],
|
||||
[
|
||||
@@ -310,7 +310,7 @@ class ValueObservationQueryInterfaceRequestTests: GRDBTestCase {
|
||||
// The fundamental technique for removing duplicates of non-Equatable types
|
||||
try assertValueObservation(
|
||||
ValueObservation
|
||||
.tracking { db in try Row.fetchAll(db, request) }
|
||||
.trackingConstantRegion { db in try Row.fetchAll(db, request) }
|
||||
.removeDuplicates()
|
||||
.map { rows in rows.map(ParentInfo.init(row:)) },
|
||||
records: [
|
||||
|
||||
@@ -5,7 +5,7 @@ class ValueObservationReadonlyTests: GRDBTestCase {
|
||||
|
||||
func testReadOnlyObservation() throws {
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking {
|
||||
ValueObservation.trackingConstantRegion {
|
||||
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
|
||||
},
|
||||
records: [0, 1],
|
||||
@@ -19,7 +19,7 @@ class ValueObservationReadonlyTests: GRDBTestCase {
|
||||
|
||||
func testWriteObservationFailsByDefaultWithErrorHandling() throws {
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking { db -> Int in
|
||||
ValueObservation.trackingConstantRegion { db -> Int in
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
return 0
|
||||
},
|
||||
@@ -35,7 +35,7 @@ class ValueObservationReadonlyTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
func testWriteObservation() throws {
|
||||
var observation = ValueObservation.tracking { db -> Int in
|
||||
var observation = ValueObservation.trackingConstantRegion { db -> Int in
|
||||
XCTAssert(db.isInsideTransaction, "expected a wrapping transaction")
|
||||
try db.execute(sql: "CREATE TEMPORARY TABLE temp AS SELECT * FROM t")
|
||||
let result = try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM temp")!
|
||||
@@ -57,7 +57,7 @@ class ValueObservationReadonlyTests: GRDBTestCase {
|
||||
|
||||
func testWriteObservationIsWrappedInSavepointWithErrorHandling() throws {
|
||||
struct TestError: Error { }
|
||||
var observation = ValueObservation.tracking { db in
|
||||
var observation = ValueObservation.trackingConstantRegion { db in
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
throw TestError()
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ class ValueObservationRecordTests: GRDBTestCase {
|
||||
let request = SQLRequest<Player>(sql: "SELECT * FROM t ORDER BY id")
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchAll),
|
||||
ValueObservation.trackingConstantRegion(request.fetchAll),
|
||||
records: [
|
||||
[],
|
||||
[Player(id: 1, name: "foo")],
|
||||
@@ -43,7 +43,7 @@ class ValueObservationRecordTests: GRDBTestCase {
|
||||
// The fundamental technique for removing duplicates of non-Equatable types
|
||||
try assertValueObservation(
|
||||
ValueObservation
|
||||
.tracking { try Row.fetchAll($0, request) }
|
||||
.trackingConstantRegion { try Row.fetchAll($0, request) }
|
||||
.removeDuplicates()
|
||||
.map { $0.map(Player.init(row:)) },
|
||||
records: [
|
||||
@@ -71,7 +71,7 @@ class ValueObservationRecordTests: GRDBTestCase {
|
||||
let request = SQLRequest<Player>(sql: "SELECT * FROM t ORDER BY id DESC")
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchOne),
|
||||
ValueObservation.trackingConstantRegion(request.fetchOne),
|
||||
records: [
|
||||
nil,
|
||||
Player(id: 1, name: "foo"),
|
||||
@@ -98,7 +98,7 @@ class ValueObservationRecordTests: GRDBTestCase {
|
||||
// The fundamental technique for removing duplicates of non-Equatable types
|
||||
try assertValueObservation(
|
||||
ValueObservation
|
||||
.tracking { try Row.fetchOne($0, request) }
|
||||
.trackingConstantRegion { try Row.fetchOne($0, request) }
|
||||
.removeDuplicates()
|
||||
.map { $0.map(Player.init(row:)) },
|
||||
records: [
|
||||
|
||||
@@ -110,7 +110,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
// I'm completely paranoid about tuple destructuring - I can't wrap my
|
||||
// head about the rules that allow or disallow it.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let observation = ValueObservation.tracking { db -> (Int, String) in
|
||||
let observation = ValueObservation.trackingConstantRegion { db -> (Int, String) in
|
||||
(0, "")
|
||||
}
|
||||
_ = observation.start(
|
||||
@@ -137,7 +137,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
|
||||
var regions: [DatabaseRegion] = []
|
||||
let observation = ValueObservation
|
||||
.trackingVaryingRegion({ db -> Int in
|
||||
.tracking({ db -> Int in
|
||||
let table = try String.fetchOne(db, sql: "SELECT name FROM source")!
|
||||
return try Int.fetchOne(db, sql: "SELECT IFNULL(SUM(value), 0) FROM \(table)")!
|
||||
})
|
||||
@@ -188,7 +188,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
|
||||
var regions: [DatabaseRegion] = []
|
||||
let observation = ValueObservation
|
||||
.trackingVaryingRegion({ db -> Int in
|
||||
.tracking({ db -> Int in
|
||||
let table = try String.fetchOne(db, sql: "SELECT name FROM source")!
|
||||
return try Int.fetchOne(db, sql: "SELECT IFNULL(SUM(value), 0) FROM \(table)")!
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ class ValueObservationRowTests: GRDBTestCase {
|
||||
let request = SQLRequest<Row>(sql: "SELECT * FROM t ORDER BY id")
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchAll),
|
||||
ValueObservation.trackingConstantRegion(request.fetchAll),
|
||||
records: [
|
||||
[],
|
||||
[["id":1, "name":"foo"]],
|
||||
@@ -29,7 +29,7 @@ class ValueObservationRowTests: GRDBTestCase {
|
||||
})
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchAll).removeDuplicates(),
|
||||
ValueObservation.trackingConstantRegion(request.fetchAll).removeDuplicates(),
|
||||
records: [
|
||||
[],
|
||||
[["id":1, "name":"foo"]],
|
||||
@@ -55,7 +55,7 @@ class ValueObservationRowTests: GRDBTestCase {
|
||||
let request = SQLRequest<Row>(sql: "SELECT * FROM t ORDER BY id DESC")
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchOne),
|
||||
ValueObservation.trackingConstantRegion(request.fetchOne),
|
||||
records: [
|
||||
nil,
|
||||
["id":1, "name":"foo"],
|
||||
@@ -80,7 +80,7 @@ class ValueObservationRowTests: GRDBTestCase {
|
||||
})
|
||||
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(request.fetchOne).removeDuplicates(),
|
||||
ValueObservation.trackingConstantRegion(request.fetchOne).removeDuplicates(),
|
||||
records: [
|
||||
nil,
|
||||
["id":1, "name":"foo"],
|
||||
@@ -106,7 +106,7 @@ class ValueObservationRowTests: GRDBTestCase {
|
||||
|
||||
func testFTS4Observation() throws {
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(SQLRequest<Row>(sql: "SELECT * FROM ft_documents").fetchAll),
|
||||
ValueObservation.trackingConstantRegion(SQLRequest<Row>(sql: "SELECT * FROM ft_documents").fetchAll),
|
||||
records: [
|
||||
[],
|
||||
[["content":"foo"]]],
|
||||
@@ -120,7 +120,7 @@ class ValueObservationRowTests: GRDBTestCase {
|
||||
|
||||
func testSynchronizedFTS4Observation() throws {
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(SQLRequest<Row>(sql: "SELECT * FROM ft_documents").fetchAll),
|
||||
ValueObservation.trackingConstantRegion(SQLRequest<Row>(sql: "SELECT * FROM ft_documents").fetchAll),
|
||||
records: [
|
||||
[],
|
||||
[["content":"foo"]]],
|
||||
@@ -141,7 +141,7 @@ class ValueObservationRowTests: GRDBTestCase {
|
||||
|
||||
func testJoinedFTS4Observation() throws {
|
||||
try assertValueObservation(
|
||||
ValueObservation.tracking(SQLRequest<Row>(sql: """
|
||||
ValueObservation.trackingConstantRegion(SQLRequest<Row>(sql: """
|
||||
SELECT document.* FROM document
|
||||
JOIN ft_document ON ft_document.rowid = document.id
|
||||
WHERE ft_document MATCH 'foo'
|
||||
|
||||
@@ -7,7 +7,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
func test(_ dbWriter: DatabaseWriter) throws {
|
||||
// Create an observation
|
||||
struct TestError: Error { }
|
||||
let observation = ValueObservation.tracking { _ in throw TestError() }
|
||||
let observation = ValueObservation.trackingConstantRegion { _ in throw TestError() }
|
||||
|
||||
// Start observation
|
||||
var error: TestError?
|
||||
@@ -36,7 +36,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
|
||||
struct TestError: Error { }
|
||||
var nextError: Error? = nil // If not null, observation throws an error
|
||||
let observation = ValueObservation.tracking {
|
||||
let observation = ValueObservation.trackingConstantRegion {
|
||||
_ = try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")
|
||||
if let error = nextError {
|
||||
throw error
|
||||
@@ -93,7 +93,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
var region: DatabaseRegion?
|
||||
let expectation = self.expectation(description: "")
|
||||
let observation = ValueObservation
|
||||
.tracking(request.fetchAll)
|
||||
.trackingConstantRegion(request.fetchAll)
|
||||
.handleEvents(willTrackRegion: {
|
||||
region = $0
|
||||
expectation.fulfill()
|
||||
@@ -120,7 +120,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
// its first read access, and its write access that installs the
|
||||
// transaction observer, some write did happen.
|
||||
var needsChange = true
|
||||
let observation = ValueObservation.tracking { db -> Int in
|
||||
let observation = ValueObservation.trackingConstantRegion { db -> Int in
|
||||
if needsChange {
|
||||
needsChange = false
|
||||
try dbPool.write { db in
|
||||
@@ -160,7 +160,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
// its first read access, and its write access that installs the
|
||||
// transaction observer, some write did happen.
|
||||
var needsChange = true
|
||||
let observation = ValueObservation.tracking { db -> Int in
|
||||
let observation = ValueObservation.trackingConstantRegion { db -> Int in
|
||||
if needsChange {
|
||||
needsChange = false
|
||||
try dbPool.write { db in
|
||||
@@ -200,7 +200,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
// its first read access, and its write access that installs the
|
||||
// transaction observer, no write did happen.
|
||||
var needsChange = true
|
||||
let observation = ValueObservation.tracking { db -> Int in
|
||||
let observation = ValueObservation.trackingConstantRegion { db -> Int in
|
||||
if needsChange {
|
||||
needsChange = false
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
||||
@@ -259,7 +259,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
// its first read access, and its write access that installs the
|
||||
// transaction observer, no write did happen.
|
||||
var needsChange = true
|
||||
let observation = ValueObservation.tracking { db -> Int in
|
||||
let observation = ValueObservation.trackingConstantRegion { db -> Int in
|
||||
if needsChange {
|
||||
needsChange = false
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
||||
@@ -322,7 +322,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
// Create an observation
|
||||
let observation = ValueObservation.tracking {
|
||||
let observation = ValueObservation.trackingConstantRegion {
|
||||
try Int.fetchOne($0, sql: "SELECT * FROM t")
|
||||
}
|
||||
|
||||
@@ -368,7 +368,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
// Create an observation
|
||||
let observation = ValueObservation.tracking {
|
||||
let observation = ValueObservation.trackingConstantRegion {
|
||||
try Int.fetchOne($0, sql: "SELECT * FROM t")
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user