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 providesregisterObserverWithSignalNext, 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)
UseregisterObserverWithSignalNext 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
registerObserverWithSignalNext instead.Choosing the Right Observer Method
| Scenario | Recommended Method |
|---|---|
| High-frequency updates (sensors, real-time tracking) | registerObserverWithSignalNext |
| UI updates that need render-cycle synchronization | registerObserverWithSignalNext |
| Async processing that may take time | registerObserverWithSignalNext |
| Low-frequency updates (user profile changes) | registerObserver |
| Simple use cases with infrequent changes | registerObserver |
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, callcancel on the observer object.
Once canceled, the store observer will stop processing in the background and will no longer call the provided callback.