mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Clean up ValueObservation fetching system
This commit is contained in:
1 parent
416d2cae1e
commit
da042eaae9
5 files changed
+84
-69
No files matched your search
@@ -107,7 +107,7 @@ extension DatabaseSnapshot {
|
||||
// Deal with initial value
|
||||
switch observation.scheduling {
|
||||
case .mainQueue:
|
||||
if let value = try unsafeReentrantRead(observation.initialValue) {
|
||||
if let value = try unsafeReentrantRead(observation.fetchFirst) {
|
||||
if DispatchQueue.isMain {
|
||||
onChange(value)
|
||||
} else {
|
||||
@@ -118,7 +118,7 @@ extension DatabaseSnapshot {
|
||||
}
|
||||
case let .async(onQueue: queue, startImmediately: startImmediately):
|
||||
if startImmediately {
|
||||
if let value = try unsafeReentrantRead(observation.initialValue) {
|
||||
if let value = try unsafeReentrantRead(observation.fetchFirst) {
|
||||
queue.async {
|
||||
onChange(value)
|
||||
}
|
||||
@@ -126,7 +126,7 @@ extension DatabaseSnapshot {
|
||||
}
|
||||
case let .unsafe(startImmediately: startImmediately):
|
||||
if startImmediately {
|
||||
if let value = try unsafeReentrantRead(observation.initialValue) {
|
||||
if let value = try unsafeReentrantRead(observation.fetchFirst) {
|
||||
onChange(value)
|
||||
}
|
||||
}
|
||||
@@ -141,15 +141,6 @@ extension DatabaseSnapshot {
|
||||
}
|
||||
}
|
||||
|
||||
extension ValueObservation where Reducer: ValueReducer {
|
||||
/// Helper method for DatabaseSnapshot.add(observation:onError:onChange:)
|
||||
fileprivate func initialValue(_ db: Database) throws -> Reducer.Value? {
|
||||
var reducer = try makeReducer(db)
|
||||
let fetched = try reducer.fetch(db)
|
||||
return reducer.value(fetched)
|
||||
}
|
||||
}
|
||||
|
||||
/// An observer that does nothing, support for
|
||||
/// `DatabaseSnapshot.add(observation:onError:onChange:)`.
|
||||
private class SnapshotValueObserver: TransactionObserver {
|
||||
|
||||
@@ -203,7 +203,7 @@ extension DatabaseWriter {
|
||||
// any future transaction can trigger a change.
|
||||
switch observation.scheduling {
|
||||
case .mainQueue:
|
||||
if let value = try reducer.initialValue(db, requiresWriteAccess: observation.requiresWriteAccess) {
|
||||
if let value = try reducer.value(reducer.fetch(db, requiringWriteAccess: observation.requiresWriteAccess)) {
|
||||
if calledOnMainQueue {
|
||||
startValue = value
|
||||
} else {
|
||||
@@ -212,13 +212,13 @@ extension DatabaseWriter {
|
||||
}
|
||||
case let .async(onQueue: queue, startImmediately: startImmediately):
|
||||
if startImmediately {
|
||||
if let value = try reducer.initialValue(db, requiresWriteAccess: observation.requiresWriteAccess) {
|
||||
if let value = try reducer.value(reducer.fetch(db, requiringWriteAccess: observation.requiresWriteAccess)) {
|
||||
queue.async { onChange(value) }
|
||||
}
|
||||
}
|
||||
case let .unsafe(startImmediately: startImmediately):
|
||||
if startImmediately {
|
||||
startValue = try reducer.initialValue(db, requiresWriteAccess: observation.requiresWriteAccess)
|
||||
startValue = try reducer.value(reducer.fetch(db, requiringWriteAccess: observation.requiresWriteAccess))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,9 +226,10 @@ extension DatabaseWriter {
|
||||
let valueObserver = try ValueObserver(
|
||||
region: observation.observedRegion(db),
|
||||
reducer: reducer,
|
||||
configuration: db.configuration,
|
||||
fetch: observation.fetchAfterChange(in: self),
|
||||
requiresWriteAccess: observation.requiresWriteAccess,
|
||||
writer: self,
|
||||
notificationQueue: observation.notificationQueue,
|
||||
reduceQueue: db.configuration.makeDispatchQueue(defaultLabel: "GRDB", purpose: "ValueObservation.reducer"),
|
||||
onError: onError,
|
||||
onChange: onChange)
|
||||
db.add(transactionObserver: valueObserver, extent: .observerLifetime)
|
||||
@@ -238,48 +239,6 @@ extension DatabaseWriter {
|
||||
}
|
||||
}
|
||||
|
||||
extension ValueReducer {
|
||||
/// Helper method for DatabaseWriter.add(observation:onError:onChange:)
|
||||
fileprivate mutating func initialValue(_ db: Database, requiresWriteAccess: Bool) throws -> Value? {
|
||||
if requiresWriteAccess {
|
||||
var fetchedValue: Fetched!
|
||||
try db.inSavepoint {
|
||||
fetchedValue = try fetch(db)
|
||||
return .commit
|
||||
}
|
||||
return value(fetchedValue)
|
||||
} else {
|
||||
return try value(db.readOnly { try fetch(db) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ValueObservation where Reducer: ValueReducer {
|
||||
/// Helper method for DatabaseWriter.add(observation:onError:onChange:)
|
||||
fileprivate func fetchAfterChange(in writer: DatabaseWriter) -> (Database, Reducer) -> DatabaseFuture<Reducer.Fetched> {
|
||||
// The technique to return a future value after database has changed
|
||||
// depends on the requiresWriteAccess flag:
|
||||
if requiresWriteAccess {
|
||||
// Synchronous fetch
|
||||
return { (db, reducer) in
|
||||
DatabaseFuture(Result {
|
||||
var fetchedValue: Reducer.Fetched!
|
||||
try db.inTransaction {
|
||||
fetchedValue = try reducer.fetch(db)
|
||||
return .commit
|
||||
}
|
||||
return fetchedValue
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// Concurrent fetch
|
||||
return { [unowned writer] (_, reducer) in
|
||||
writer.concurrentRead(reducer.fetch)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A future database value, returned by the DatabaseWriter.concurrentRead(_:)
|
||||
/// method.
|
||||
///
|
||||
|
||||
@@ -196,6 +196,31 @@ extension ValueObservation where Reducer: ValueReducer {
|
||||
{
|
||||
return try reader.add(observation: self, onError: onError, onChange: onChange)
|
||||
}
|
||||
|
||||
// MARK: - Fetching Values
|
||||
|
||||
/// Returns the observed value.
|
||||
///
|
||||
/// This method returns nil if observation would not notify any
|
||||
/// initial value.
|
||||
///
|
||||
/// For example, the observation below notifies changes to a player if and
|
||||
/// only if it exists:
|
||||
///
|
||||
/// let request = Player.filter(key: 42)
|
||||
/// let observation = ValueObservation
|
||||
/// .trackingOne(request)
|
||||
/// .compactMap { $0 } // filters out missing player
|
||||
///
|
||||
/// The `fetchFirst` method thus returns nil if player does not exist:
|
||||
///
|
||||
/// let player: Player? = try dbQueue.read { db in
|
||||
/// try observation.fetchFirst(db)
|
||||
/// }
|
||||
func fetchFirst(_ db: Database) throws -> Reducer.Value? {
|
||||
var reducer = try makeReducer(db)
|
||||
return try reducer.value(reducer.fetch(db, requiringWriteAccess: requiresWriteAccess))
|
||||
}
|
||||
}
|
||||
|
||||
extension ValueObservation {
|
||||
|
||||
@@ -5,29 +5,32 @@ import Foundation
|
||||
class ValueObserver<Reducer: ValueReducer>: TransactionObserver {
|
||||
/* private */ let region: DatabaseRegion // Internal for testability
|
||||
private var reducer: Reducer
|
||||
private let fetch: (Database, Reducer) -> DatabaseFuture<Reducer.Fetched>
|
||||
private var requiresWriteAccess: Bool
|
||||
unowned private var writer: DatabaseWriter
|
||||
private let notificationQueue: DispatchQueue?
|
||||
private let reduceQueue: DispatchQueue
|
||||
private let onError: ((Error) -> Void)?
|
||||
private let onChange: (Reducer.Value) -> Void
|
||||
private let reduceQueue: DispatchQueue
|
||||
private var isChanged = false
|
||||
|
||||
init(
|
||||
region: DatabaseRegion,
|
||||
reducer: Reducer,
|
||||
configuration: Configuration,
|
||||
fetch: @escaping (Database, Reducer) -> DatabaseFuture<Reducer.Fetched>,
|
||||
requiresWriteAccess: Bool,
|
||||
writer: DatabaseWriter,
|
||||
notificationQueue: DispatchQueue?,
|
||||
reduceQueue: DispatchQueue,
|
||||
onError: ((Error) -> Void)?,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
{
|
||||
self.writer = writer
|
||||
self.region = region
|
||||
self.reducer = reducer
|
||||
self.fetch = fetch
|
||||
self.requiresWriteAccess = requiresWriteAccess
|
||||
self.notificationQueue = notificationQueue
|
||||
self.onChange = onChange
|
||||
self.reduceQueue = reduceQueue
|
||||
self.onError = onError
|
||||
self.reduceQueue = configuration.makeDispatchQueue(defaultLabel: "GRDB", purpose: "ValueObservation.reducer")
|
||||
self.onChange = onChange
|
||||
}
|
||||
|
||||
func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool {
|
||||
@@ -45,10 +48,10 @@ class ValueObserver<Reducer: ValueReducer>: TransactionObserver {
|
||||
guard isChanged else { return }
|
||||
isChanged = false
|
||||
|
||||
// Grab future fetched values from the database writer queue
|
||||
let future = fetch(db, reducer)
|
||||
// Grab future fetched value
|
||||
let future = reducer.fetchFuture(db, writer: writer, requiringWriteAccess: requiresWriteAccess)
|
||||
|
||||
// Wait for future fetched values in reduceQueue. This guarantees:
|
||||
// Wait for future fetched value in reduceQueue. This guarantees:
|
||||
// - that notifications have the same ordering as transactions.
|
||||
// - that expensive reduce operations are computed without blocking
|
||||
// any database dispatch queue.
|
||||
|
||||
@@ -18,6 +18,43 @@ public protocol ValueReducer {
|
||||
mutating func value(_ fetched: Fetched) -> Value?
|
||||
}
|
||||
|
||||
extension ValueReducer {
|
||||
/// Synchronous fetch
|
||||
func fetch(_ db: Database, requiringWriteAccess: Bool) throws -> Fetched {
|
||||
if requiringWriteAccess {
|
||||
var fetchedValue: Fetched?
|
||||
try db.inSavepoint {
|
||||
fetchedValue = try fetch(db)
|
||||
return .commit
|
||||
}
|
||||
return fetchedValue!
|
||||
} else {
|
||||
return try db.readOnly {
|
||||
try fetch(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Synchronous or asynchronous fetch
|
||||
/// Support for ValueObserver
|
||||
func fetchFuture(_ db: Database, writer: DatabaseWriter, requiringWriteAccess: Bool) -> DatabaseFuture<Fetched> {
|
||||
if requiringWriteAccess {
|
||||
// Synchronous fetch
|
||||
return DatabaseFuture(Result {
|
||||
var fetchedValue: Fetched!
|
||||
try db.inTransaction {
|
||||
fetchedValue = try fetch(db)
|
||||
return .commit
|
||||
}
|
||||
return fetchedValue
|
||||
})
|
||||
} else {
|
||||
// Concurrent fetch
|
||||
return writer.concurrentRead(fetch)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// A type-erased ValueReducer.
|
||||
|
||||
Reference in new issue
Block a user