Understanding Observer Backpressure
When using store observers, callbacks fire every time matching data changes in your local store. In high-frequency scenarios—such as IoT sensors publishing multiple times per second or real-time position tracking—callbacks can fire faster than your application can process them. This leads to a backlog of pending callbacks, excessive memory consumption, and potential crashes. To handle this, Ditto providesregisterObserver with DittoChangeListenerWithNextSignal, which gives you explicit control over when the observer can deliver the next update. Instead of automatic callback delivery, you call a signalNext function when your application is ready for the next batch of changes.
Setting Up Store Observers
Store Observer with Backpressure Control (Recommended)
UseregisterObserver with DittoChangeListenerWithNextSignal to control when your observer receives updates. The callback includes a signalNext function that you call when ready for the next update.
Basic Store Observer
For low-frequency updates where backpressure is not a concern, use the simplerregisterObserver method.
This approach does not provide backpressure control. If updates arrive faster
than your callback can process them, callbacks will queue up and may cause
memory issues. For high-frequency scenarios, use
registerObserver with
DittoChangeListenerWithNextSignal instead.Choosing the Right Observer Method
| Scenario | Recommended Method |
|---|---|
| High-frequency updates (sensors, real-time tracking) | registerObserver with DittoChangeListenerWithNextSignal |
| UI updates that need render-cycle synchronization | registerObserver with DittoChangeListenerWithNextSignal |
| Async processing that may take time | registerObserver with DittoChangeListenerWithNextSignal |
| Low-frequency updates (user profile changes) | registerObserver with DittoChangeListener |
| Simple use cases with infrequent changes | registerObserver with DittoChangeListener |
For more information on memory management with observers, see the Diffing
Results section which covers
QueryResult lifecycle and proper cleanup patterns.Canceling a Store Observer
To cancel a store observer, callclose() on the observer object.
Once canceled, the store observer will stop processing in the background and will no longer call the provided callback.