-
Consider two model objects, a vehicle: extension Vehicle: FetchableRecord, PersistableRecord {
static let scans = hasMany(VehicleScan.self)
var scans: QueryInterfaceRequest<VehicleScan> {
request(for: Vehicle.scans)
}
} and a scan: extension VehicleScan: FetchableRecord, PersistableRecord {
static let vehicle = belongsTo(Vehicle.self)
} Following your example, creating a struct AllVehicles: Queryable {
static var defaultValue: [Vehicle] { [] }
func publisher(in dbQueue: DatabaseQueue) -> AnyPublisher<[Vehicle], Error> {
ValueObservation
.tracking(Vehicle.fetchAll)
.publisher(in: dbQueue, scheduling: .immediate)
.eraseToAnyPublisher()
}
} I'm not quite sure though how to construct a struct AllScansForVehicle: Queryable {
let vehicle: Vehicle
init(_ vehicle: Vehicle) {
self.vehicle = vehicle
}
static var defaultValue: [VehicleScan] { [] }
func publisher(in dbQueue: DatabaseQueue) -> AnyPublisher<[VehicleScan], Error> {
ValueObservation
.tracking(self.vehicle.scans.fetchAll)
.publisher(in: dbQueue, scheduling: .immediate)
.eraseToAnyPublisher()
}
} But — of course – I can't instantiate that in SwiftUI: struct VehicleDetailsView: View {
@ObservedObject var vehicle: Vehicle
@Query(AllScansForVehicle(vehicle), in: \.dbQueue) var scans: [VehicleScan]
^^^^Cannot use instance member 'vehicle' within property initializer; property initializers run before 'self' is available
…
} How could I achieve that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @mickeyl, I know a technique, shown in one of the GRDB demo apps: https://github.com/groue/GRDB.swift/blob/v5.22.0/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/AppView.swift#L18 In your app, this should give: struct VehicleDetailsView: View {
@ObservedObject var vehicle: Vehicle
@Query<AllScansForVehicle> var scans: [VehicleScan]
init(vehicle: Vehicle) {
self.vehicle = vehicle
_scans = Query(AllScansForVehicle(vehicle), in: \.dbQueue)
}
} This technique can be extended to all other property wrappers that need a custom initialization! |
Beta Was this translation helpful? Give feedback.
Hello @mickeyl,
I know a technique, shown in one of the GRDB demo apps: https://github.com/groue/GRDB.swift/blob/v5.22.0/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/AppView.swift#L18
In your app, this should give:
This technique can be extended to all other property wrappers that need a custom initialization!