mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Refactor Query for AppDatabase access
This commit is contained in:
1 parent
ed8ff31b3e
commit
1e17bf011f
4 files changed
+46
-48
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import Combine
|
||||
import GRDB
|
||||
|
||||
/// A player request defines how to feed the player list
|
||||
@@ -14,10 +15,17 @@ struct PlayerRequest {
|
||||
extension PlayerRequest: Queryable {
|
||||
static var defaultValue: [Player] { [] }
|
||||
|
||||
func fetchValue(_ db: Database) throws -> [Player] {
|
||||
switch ordering {
|
||||
case .byScore: return try Player.all().orderedByScore().fetchAll(db)
|
||||
case .byName: return try Player.all().orderedByName().fetchAll(db)
|
||||
}
|
||||
func valuePublisher(in appDatabase: AppDatabase) -> AnyPublisher<[Player], Error> {
|
||||
ValueObservation
|
||||
.trackingConstantRegion { db in
|
||||
switch ordering {
|
||||
case .byScore:
|
||||
return try Player.all().orderedByScore().fetchAll(db)
|
||||
case .byName:
|
||||
return try Player.all().orderedByName().fetchAll(db)
|
||||
}
|
||||
}
|
||||
.publisher(in: appDatabase.databaseReader, scheduling: .immediate)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
@@ -17,15 +17,15 @@ protocol Queryable: Equatable {
|
||||
/// The default value, used whenever the database is not available
|
||||
static var defaultValue: Value { get }
|
||||
|
||||
/// Fetches the database value
|
||||
func fetchValue(_ db: Database) throws -> Value
|
||||
/// Returns a publisher of database values
|
||||
func valuePublisher(in appDatabase: AppDatabase) -> AnyPublisher<Value, Error>
|
||||
}
|
||||
|
||||
/// The property wrapper that observes a database query
|
||||
@propertyWrapper
|
||||
struct Query<Query: Queryable>: DynamicProperty {
|
||||
/// The database reader that makes it possible to observe the database
|
||||
@Environment(\.appDatabase?.databaseReader) private var databaseReader: DatabaseReader?
|
||||
/// The AppDatabase that grants access to the database
|
||||
@Environment(\.appDatabase) private var appDatabase: AppDatabase?
|
||||
@StateObject private var core = Core()
|
||||
private var baseQuery: Query
|
||||
|
||||
@@ -51,17 +51,17 @@ struct Query<Query: Queryable>: DynamicProperty {
|
||||
}
|
||||
|
||||
func update() {
|
||||
guard let databaseReader = databaseReader else {
|
||||
guard let appDatabase = appDatabase else {
|
||||
fatalError("Attempting to use @Query without any database in the environment")
|
||||
}
|
||||
// Feed core with necessary information, and make sure tracking has started
|
||||
if core.usesBaseQuery { core.query = baseQuery }
|
||||
core.startTrackingIfNecessary(in: databaseReader)
|
||||
core.startTrackingIfNecessary(in: appDatabase)
|
||||
}
|
||||
|
||||
private class Core: ObservableObject {
|
||||
private(set) var value: Query.Value?
|
||||
var databaseReader: DatabaseReader?
|
||||
var appDatabase: AppDatabase?
|
||||
var usesBaseQuery = true
|
||||
var query: Query? {
|
||||
willSet {
|
||||
@@ -76,13 +76,7 @@ struct Query<Query: Queryable>: DynamicProperty {
|
||||
|
||||
init() { }
|
||||
|
||||
func startTrackingIfNecessary(in databaseReader: DatabaseReader) {
|
||||
if databaseReader !== self.databaseReader {
|
||||
// Database has changed. Stop tracking.
|
||||
self.databaseReader = databaseReader
|
||||
cancellable = nil
|
||||
}
|
||||
|
||||
func startTrackingIfNecessary(in appDatabase: AppDatabase) {
|
||||
guard let query = query else {
|
||||
// No query set
|
||||
return
|
||||
@@ -93,11 +87,8 @@ struct Query<Query: Queryable>: DynamicProperty {
|
||||
return
|
||||
}
|
||||
|
||||
cancellable = ValueObservation
|
||||
.tracking(query.fetchValue)
|
||||
.publisher(
|
||||
in: databaseReader,
|
||||
scheduling: .immediate)
|
||||
cancellable = query
|
||||
.valuePublisher(in: appDatabase)
|
||||
.sink(
|
||||
receiveCompletion: { _ in
|
||||
// Ignore errors
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import Combine
|
||||
import GRDB
|
||||
|
||||
/// A player request defines how to feed the player list
|
||||
@@ -14,10 +15,17 @@ struct PlayerRequest {
|
||||
extension PlayerRequest: Queryable {
|
||||
static var defaultValue: [Player] { [] }
|
||||
|
||||
func fetchValue(_ db: Database) throws -> [Player] {
|
||||
switch ordering {
|
||||
case .byScore: return try Player.all().orderedByScore().fetchAll(db)
|
||||
case .byName: return try Player.all().orderedByName().fetchAll(db)
|
||||
}
|
||||
func valuePublisher(in appDatabase: AppDatabase) -> AnyPublisher<[Player], Error> {
|
||||
ValueObservation
|
||||
.trackingConstantRegion { db in
|
||||
switch ordering {
|
||||
case .byScore:
|
||||
return try Player.all().orderedByScore().fetchAll(db)
|
||||
case .byName:
|
||||
return try Player.all().orderedByName().fetchAll(db)
|
||||
}
|
||||
}
|
||||
.publisher(in: appDatabase.databaseReader, scheduling: .immediate)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
@@ -17,15 +17,15 @@ protocol Queryable: Equatable {
|
||||
/// The default value, used whenever the database is not available
|
||||
static var defaultValue: Value { get }
|
||||
|
||||
/// Fetches the database value
|
||||
func fetchValue(_ db: Database) throws -> Value
|
||||
/// Returns a publisher of database values
|
||||
func valuePublisher(in appDatabase: AppDatabase) -> AnyPublisher<Value, Error>
|
||||
}
|
||||
|
||||
/// The property wrapper that observes a database query
|
||||
@propertyWrapper
|
||||
struct Query<Query: Queryable>: DynamicProperty {
|
||||
/// The database reader that makes it possible to observe the database
|
||||
@Environment(\.appDatabase?.databaseReader) private var databaseReader: DatabaseReader?
|
||||
/// The AppDatabase that grants access to the database
|
||||
@Environment(\.appDatabase) private var appDatabase: AppDatabase?
|
||||
@StateObject private var core = Core()
|
||||
private var baseQuery: Query
|
||||
|
||||
@@ -51,17 +51,17 @@ struct Query<Query: Queryable>: DynamicProperty {
|
||||
}
|
||||
|
||||
func update() {
|
||||
guard let databaseReader = databaseReader else {
|
||||
guard let appDatabase = appDatabase else {
|
||||
fatalError("Attempting to use @Query without any database in the environment")
|
||||
}
|
||||
// Feed core with necessary information, and make sure tracking has started
|
||||
if core.usesBaseQuery { core.query = baseQuery }
|
||||
core.startTrackingIfNecessary(in: databaseReader)
|
||||
core.startTrackingIfNecessary(in: appDatabase)
|
||||
}
|
||||
|
||||
private class Core: ObservableObject {
|
||||
private(set) var value: Query.Value?
|
||||
var databaseReader: DatabaseReader?
|
||||
var appDatabase: AppDatabase?
|
||||
var usesBaseQuery = true
|
||||
var query: Query? {
|
||||
willSet {
|
||||
@@ -76,13 +76,7 @@ struct Query<Query: Queryable>: DynamicProperty {
|
||||
|
||||
init() { }
|
||||
|
||||
func startTrackingIfNecessary(in databaseReader: DatabaseReader) {
|
||||
if databaseReader !== self.databaseReader {
|
||||
// Database has changed. Stop tracking.
|
||||
self.databaseReader = databaseReader
|
||||
cancellable = nil
|
||||
}
|
||||
|
||||
func startTrackingIfNecessary(in appDatabase: AppDatabase) {
|
||||
guard let query = query else {
|
||||
// No query set
|
||||
return
|
||||
@@ -93,11 +87,8 @@ struct Query<Query: Queryable>: DynamicProperty {
|
||||
return
|
||||
}
|
||||
|
||||
cancellable = ValueObservation
|
||||
.tracking(query.fetchValue)
|
||||
.publisher(
|
||||
in: databaseReader,
|
||||
scheduling: .immediate)
|
||||
cancellable = query
|
||||
.valuePublisher(in: appDatabase)
|
||||
.sink(
|
||||
receiveCompletion: { _ in
|
||||
// Ignore errors
|
||||
|
||||
Reference in new issue
Block a user