Hi, I am working on my first project (in macOS) with GRDB. So far so, good, mostly!
I'm at the point where I'm ready to start implementing a ValueObservation to keep an eye on the value of one particular field in the database. If the value changes, then I need to refresh the UI of my app.
The examples in the documentation aren't spot-on what I'm trying to do, and in any case I'm getting a lot of Xcode errors (particularly that consecutive statements must be separated by a comma) with the sample code. What I have so far, and what seems to mostly work for me, is the following:
import Cocoa
class RaceStatusViewController: NSViewController {
@IBOutlet var dashboardWindowController: DashboardWindowController?
private var observer: TransactionObserver?
var carRaceDetailsViewController: CarRaceDetailsViewController!
override func viewDidLoad() {
super.viewDidLoad()
let request = SQLRequest<Row>("SELECT ItemValue FROM RaceInfo WHERE ItemKey= ?", arguments: ["Heat"])
let observation = ValueObservation.trackingOne(request)
observer = try! observation.start(in: dbQueue, onChange: { _ in
print("Database value has changed")
})
When I run the app, the condition appears to be successful, as I see "Database value has changed" printed to the console. Eventually, I would replace this statement with a function call to refresh the screen, but this is enough for testing.
But once the app is up and running, subsequent changes to the field I'm watching do not result in the print statement being executed again. I'm not sure if this matters, but I would note that the changes are being made by an outside application -- my app is watching (on a read-only basis) a database that's being run and populated by an outside app.
What am I missing? Is this the right tool for this type of an observation?
Thanks!
Matt