Replies: 1 comment 1 reply
-
Hello @TeamAddis,
The values produced by There are a lot of ways to perform database writes from the view, with various level of architectural complexity. The most straightforward is probably to access the database from the SwiftUI environment. Something along: struct MyView {
@Environment(\.dbQueue) var dbQueue
@State selectedPlayer: Player?
var body: some View {
...onChange(of: selectedPlayer) {
dbQueue.write { ... }
}
}
} This sample code was not compiled, not tested, and is not even recommended. This is just an starting point, and you will code your app as you want to code it. |
Beta Was this translation helpful? Give feedback.
-
I've run into an issue that I can't seem to design my way out of. I have a Picker in SwiftUI that is displaying a list of values. Those values are fetched using the GRDBQuery wrapper. Using the CombineDemo as a base, I've added the code below.
AppView.swift
Now this works okay. I am able to select players from the Query provided list and the selected player will be assigned to
selectedPlayer
. My question comes when I want to have this binding value prepopulated with a previously selected or default player.Player.swift
I figured I could update the Player record to keep track of which player is currently selected. After performing DB migrations I create a random list of players with one of those players
selected = true
.I then created a new request to allow me to update the selectedPlayer code in ContentView.swift.
AppView.swift (changes only)
This then leads me to an error explaining that the Combine Publisher is read only (get) so the Picker cannot use this as the selector.
Is there a supported way to do this with GRDB? Maybe I need to do some fancy work and wrap a Query inside of an ObservableObject to allow monitoring of the DB and keeping track of the Picker's binding.
Beta Was this translation helpful? Give feedback.
All reactions