groue / GRDBCombine

GRDB ❤️ Combine
MIT License
223 stars 16 forks source link

Is it possible to briefly describe how "Database Observation" work behind the scene? #12

Closed yccheok closed 5 years ago

yccheok commented 5 years ago

I believe achieving "Database Observation" is a rather complex task, as we need to notify correct table's observers, after executing the SQL statement.

Is it possible to briefly describe the working mechanism of "Database Observation"? Is it

  1. Rely on some "built-in" features provided by SQLite, out of the box?
  2. Base on smart algorithm code developed in "GRDBCombine"?
  3. ...

So that as lib user, we can have a big picture on how it works, and aware of possible limitation.

Just FYI, this is the information, on how Android's Room LiveData works - https://www.reddit.com/r/androiddev/comments/clm1pd/does_anyone_how_the_magic_works_for_rooms_livedata/

Thanks.

groue commented 5 years ago

Hello @yccheok,

GRDB database observation relies on built-in features of SQLite:

On this base, GRBD can define the TransactionObserver protocol, which is the mother of all GRDB observation features. TransactionObserver is supported by the inner type DatabaseObservationBroker. You can read a brief description of how it works at https://github.com/groue/GRDB.swift/blob/v4.2.0/GRDB/Core/TransactionObserver.swift#L83

On top of TransactionObserver, GRDB defines all its Database Observation features.

Among those, you have ValueObservation. GRDBCombine is just a thin wrapper around it.

So that as lib user, we can have a big picture on how it works, and aware of possible limitation.

"possible limitation" is quite vague because nobody knows what you want to do. I hope the information linked above will give you the answer you look for.