Documentation Index
Fetch the complete documentation index at: https://docs.ditto.live/llms.txt
Use this file to discover all available pages before exploring further.
This content is for SDK V4. For the latest version, see the V5 documentation.
Setting Up Store Observers
Using theregisterObserver method, set up an observer within the store namespace enclosed with a query that specifies the collection to watch for changes, as well as your logic to handle the incoming changes.
Store Observer with Query Arguments
To associate arguments with your query add them as a parameter.Canceling a Store Observer
To cancel a store observer, callcancel on the observer object.
Once canceled, the store observer will stop processing in the background and will no longer call the provided callback.
Accessing Store Observers
To access store observers from the local Ditto store:Diffing Results
This feature is available in v4.11 and above.
DittoDiffer class that
allows you to opt-in to diffing only when necessary, thereby avoiding
unnecessary performance and memory costs.
However, diffing is computationally expensive. It requires storing the previous query
results in memory, leading to increased RAM usage, which can be particularly
problematic for applications handling large datasets or frequent updates.
Instead of computing diffs synchronously with every store observer update, it is
recommended to debounce diffing, processing changes out-of-band at a cadence
that best suits your use case.
A DittoDiffer is given query result items and compares them to the previous set of items it has received:
diff() method returns a DittoDiff containing:
insertions: Indexes of new items in the new arraydeletions: Indexes of items that were removed from the old arrayupdates: Indexes of items in the new array that were present in the old array and whose value has changedmoves: Pairs of indexes showing items that changed position
Example: Apple UIKit
Use this diff to update a UIKit class, such asUITableView, UICollectionView :
Swift
Key considerations
- Keep references to arrays yourself: The differ doesn’t provide access to the old and new items themselves, so they need to be retained by the user if needed.
-
No comparison of document metadata: The differ performs a deep comparison of the value of each query result item but doesn’t take into account any metadata. Applying a series of changes to a document will not cause it to show up in
updatedunless those changes result in a different value from the initial state to the final state. - No async diffing: Computing a diff on a set of many query result items or very large query result items might block the device depending on its hardware.
Migrating from Live Query Events
If you’re using the legacy live query API that automatically provides diff information throughevent parameters, you can migrate to the newer store observer pattern with the DittoDiffer class. The differ provides equivalent functionality but requires manual management of previous results.
For a complete migration guide, see Replacing Live Query Events with Store Observers.