-
I want to start by saying I'm really enjoying using GRDB and want to thank everyone for all the work that has been put into. Now on to my issue.... I am having trouble getting Here's my simplified setup:
where This part works fine. The problem is that I'm trying to observe those changes using
This I enabled SQL debugging statements by modifying my
and I can see that the INSERT statements for Here's the log. Notice that
Finally, for comparison a successful case when the INSERT statements are triggered "normally" on the dbQueue object:
Am I doing something wrong? Is this kind of functionality supported? It feels like a bug, but I'm also wondering if maybe accessing the Thanks for any and all help! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Hello @gerdemb, Thanks for the compliment! You're not doing anything wrong, but ValueObservation does not detect changes performed by Your best option is to cancel and restart the observation after the transaction that calls this function has completed. This is somewhat brutal: your app will have to process fresh values even if A rationale for the suggested solution The library relies on the Compile-Time Authorization Callbacks in order to know which tables and columns are read or written to. That's how executing We could imagine an api that allows you to make the observation mechanism less blind: // A POSSIBLE API
try dbQueue.write { db in
// Have related value observations listen to changes inside the block
try db.withChanges(to: Table("player"), Table("team")) do {
sqlite3session_apply(...)
}
} But it would still be pretty much short-sighted 😅 For example, an observation of Another problem with this technique is that statements that are not compiled by GRDB can trigger the truncate optimization. When this optimization is applied, individual deletions are not reported, and observations would not be triggered. That's another case of complete blindness. A better option might be to force some observations to fetch a fresh values (even if no actual change was performed): // ANOTHER POSSIBLE API
try dbQueue.write { db in
sqlite3session_apply(...)
// Force observations of those regions to fetch fresh values
// after transaction has been committed.
if /* we think the player table was modified */ {
try db.registerChanges(to: Table("player"))
}
if /* we think the team table was modified */ {
try db.registerChanges(to: Table("team"))
}
} But this is more or less equivalent to the initial advice: restart the observation when we think it was impacted. Maybe you have more ideas? I'm curious! |
Beta Was this translation helpful? Give feedback.
-
@groue Thank your quick response!
Thank you for confirming this and saving me time exploring down a path that I was beginning to suspect was never going to work anyway. As you suggested, I added a trigger to restart the observers after a Regarding your proposed solutions, it would be useful if GRDB offered a way to "invalidate" observations after we have modified the database in a way that GRDB cannot detect (using C API calls, etc.). In my particular case, a single call that invalidated all observations on the database would be sufficient although table-level granularity might be useful. Not sure if you would consider this a core requirement to GRDB as it can worked-around by canceling and restarting observers manually, but this is awkward. Thanks again for your response and advice. |
Beta Was this translation helpful? Give feedback.
Hello @gerdemb,
Thanks for the compliment!
You're not doing anything wrong, but ValueObservation does not detect changes performed by
sqlite3session_apply
. There is no way with the current api to make it work.Your best option is to cancel and restart the observation after the transaction that calls this function has completed. This is somewhat brutal: your app will have to process fresh values even if
sqlite3session_apply
did not actually modify the observed region. If you know the content of the change set, you may avoid useless observation restarts.A rationale for the suggested solution
The library relies on the Compile-Time Authorization Callbacks in order to know which tables and c…