ValueObservation: successful setup, but then no further notices

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

Hello @MMac,

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.

It does matter a lot! GRDB observation only works for changes performed by the observing connection.

Since your setup involves multiple connections, you'll have to look for another notification system, between your two apps.

We could discuss better support for cross-connections changes notifications, if you want. It is an interesting and valuable topic. Such a feature or pull request just hasn't showed up yet. Warning: this won't be a trivial job :wink:

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.

It looks like some areas of the doc could be improved. Don't hesitate showing them, so that they can be fixed! There is a lot of documentation, and it would be unpractical to check each and every code snippet.

Ah, OK, thanks -- that clears things up for me. There are other ways to skin this particular cat, so I'll try another approach. It's not a rapidly changing value, so I can just periodically query the db to see if the value has changed since the last time we checked.

Thanks for the quick response!
Matt

1 Like