Improve ValueObservation.trackingVaryingRegion example

This commit is contained in:
Gwendal Roué committed 2020-04-11 13:10:15 +02:00
1 parent 2c3d0b1e17
commit fea89100d9
2 files changed
+4 -2

No files matched your search

+2 -1
View File
@@ -63,7 +63,8 @@ If the tracked value is computed from several database requests that are not alw
```swift
// An observation which does not always execute the same requests:
let observation = ValueObservation.trackingVaryingRegion { db -> Int in
switch try Preference.fetchOne(db)!.selection {
let preference = try Preference.fetchOne(db) ?? .default
switch preference.selection {
case .food: return try Food.fetchCount(db)
case .beverage: return try Beverage.fetchCount(db)
}
+2 -1
View File
@@ -5634,7 +5634,8 @@ In this case, use `ValueObservation.trackingVaryingRegion(_:)` instead:
// An observation which tracks the 'preference', 'food' and 'beverage'
// tables, as needed.
let observation = ValueObservation.trackingVaryingRegion { db -> Int in
switch try Preference.fetchOne(db)!.selection {
let preference = try Preference.fetchOne(db) ?? .default
switch preference.selection {
case .food: return try Food.fetchCount(db)
case .beverage: return try Beverage.fetchCount(db)
}