mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Make sure ValueObservation fetches in an isolated transaction
Previously, ValueObservation could fetch in a non-isolated way. When an external connection would commit changes, we could fetch inconsistent values. Here we make sure ValueObservation always fetch in an isolated way.
This commit is contained in:
1 parent
ad66fbc666
commit
6240585a03
5 files changed
+23
-26
No files matched your search
@@ -393,7 +393,11 @@ extension DatabaseReader {
|
||||
{
|
||||
if scheduler.immediateInitialValue() {
|
||||
do {
|
||||
let value = try unsafeReentrantRead(observation.fetchValue)
|
||||
let value = try unsafeReentrantRead { db in
|
||||
try db.isolated(readOnly: true) {
|
||||
try observation.fetchValue(db)
|
||||
}
|
||||
}
|
||||
onChange(value)
|
||||
} catch {
|
||||
observation.events.didFail?(error)
|
||||
|
||||
@@ -212,7 +212,7 @@ extension ValueObservation: Refinable {
|
||||
/// Returns the value.
|
||||
func fetchValue(_ db: Database) throws -> Reducer.Value {
|
||||
var reducer = makeReducer()
|
||||
guard let value = try reducer.fetchAndReduce(db, requiringWriteAccess: requiresWriteAccess) else {
|
||||
guard let value = try reducer.fetchAndReduce(db) else {
|
||||
fatalError("Contract broken: reducer has no initial value")
|
||||
}
|
||||
return value
|
||||
|
||||
@@ -99,15 +99,21 @@ extension ValueObserver: TransactionObserver {
|
||||
// Synchronously
|
||||
fetchedFuture = DatabaseFuture(Result {
|
||||
try recordingSelectedRegionIfNeeded(db) {
|
||||
try synchronized {
|
||||
try reducer.fetch(db, requiringWriteAccess: requiresWriteAccess)
|
||||
try db.isolated(readOnly: !requiresWriteAccess) {
|
||||
try synchronized {
|
||||
try reducer._fetch(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// Concurrently
|
||||
guard let writer = writer else { return }
|
||||
fetchedFuture = writer.concurrentRead { db in try self.synchronized { try self.reducer._fetch(db) } }
|
||||
fetchedFuture = writer.concurrentRead { db in
|
||||
try self.synchronized {
|
||||
try self.reducer._fetch(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Reduce
|
||||
@@ -151,8 +157,10 @@ extension ValueObserver {
|
||||
/// Fetch an observed value, and moves the reducer forward.
|
||||
func fetchValue(_ db: Database) throws -> Reducer.Value? {
|
||||
try recordingSelectedRegionIfNeeded(db) {
|
||||
try synchronized {
|
||||
try reducer.fetchAndReduce(db, requiringWriteAccess: requiresWriteAccess)
|
||||
try db.isolated(readOnly: !requiresWriteAccess) {
|
||||
try synchronized {
|
||||
try reducer.fetchAndReduce(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,8 @@ extension ValueReducers {
|
||||
|
||||
/// :nodoc:
|
||||
public func _fetch(_ db: Database) throws -> Value {
|
||||
try __fetch(db)
|
||||
assert(db.isInsideTransaction, "Fetching in a non-isolated way is illegal")
|
||||
return try __fetch(db)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
|
||||
@@ -37,24 +37,8 @@ public protocol _ValueReducer {
|
||||
public protocol ValueReducer: _ValueReducer { }
|
||||
|
||||
extension ValueReducer {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mutating func fetchAndReduce(_ db: Database, requiringWriteAccess: Bool) throws -> Value? {
|
||||
let fetchedValue = try fetch(db, requiringWriteAccess: requiringWriteAccess)
|
||||
return _value(fetchedValue)
|
||||
mutating func fetchAndReduce(_ db: Database) throws -> Value? {
|
||||
try _value(_fetch(db))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user