Parallel Queries (QueriesObserver)
When an application needs to fetch multiple queries in parallel whose quantity or keys vary dynamically (e.g. fetching details for an arbitrary list of item IDs), QueriesObserver provides an aggregated observer.
Unlike QueryObserver, QueriesObserver does not observe a single key. Instead, it owns a dynamic list of child QueryObserver instances and propagates child notifications through a unified subscription.
Usage Example
dart
final queriesObserver = QueriesObserver<User, Exception>(
cache: cache,
);
// Subscribe to state updates across ALL child queries
queriesObserver.subscribe(1, () {
final observers = queriesObserver.observers;
final allLoaded = observers.every((o) => o.query.isSuccess);
if (allLoaded) {
final users = observers.map((o) => o.query.data).toList();
print('All users loaded: $users');
}
});
// Configure options dynamically
final userIds = [10, 20, 30];
queriesObserver.setOptions(
userIds.map((id) => QueryOptions<User, Exception>(
queryKey: QueryKey(['users', id]),
queryFn: () => api.fetchUser(id),
)).toList(),
);
// Clean up child observers on dispose
queriesObserver.dispose();Smart Reconciliation
When setOptions is called with updated query lists:
- Unchanged keys: The existing
QueryObserverinstance is preserved, keeping its cached data and state intact. - New keys: A new
QueryObserveris created, subscribed, and initialized. - Removed keys: Observers for omitted keys are automatically detached and disposed, allowing
QueryCacheto garbage-collect their entries if no other observers reference them.