Live Queries Management
In addition to storing individual subscription and live query objects, you can create structured and organized systems for realtime data updates by doing any of the following in your app:
- Batch multiple live queries from a single array or, if needed, a map. (Batching Multiple Live Queries)
- Create a custom class that encapsulates the logic for managing multiple live queries. (Creating and Encapsulating in a Custom Class)
For more information about subscriptions and live queries, see Platform Manual > READ. For more information about Ditto arrays and maps, see Platform Manual > Data Types.
To handle each subscription and live query individually, create separate variables for each subscription and live query:
If your subscription falls out of scope, Ditto will garbage collect it, causing sync to stop.
Rather than needing to maintain and manage multiple objects, store multiple subscriptions and live queries in arrays.
If you require a greater level of control; as in, you need the ability to add, remove, and cancel subscriptions individually, use a single map object to store multiple subscriptions instead of an array.
That way, you can look up and manage each subscription individually based on their keys. (See Platform Manual > Fields)
By storing multiple live queries in a single object, you simplify your code and streamline operations; for example:
- Once your end user logs out and your app stops, cancel all subscriptions.
- When the UI (viewModel component) closes and is no longer visible to your end user, cancel all live queries.
To batch multiple subscriptions and live queries, use arrays as follows:
For more information about LiveQuery objects, see LiveQueryEvent in the SDK Guides > Ditto API reference for your language.
Create a custom class that encapsulates your management logic.