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:
- Store each subscription and live query as a single variable. (Storing as Individual Variables)
- 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)
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:
Create a custom class that encapsulates your management logic.