-
Notifications
You must be signed in to change notification settings - Fork 0
Scopes
By default, Firestore queries and listeners operate within a general scope, which may not be ideal if you intend to reuse the same type of query efficiently across different parts of your app. This can lead to potential conflicts and inefficiencies in managing data updates.
Scopes offer various methods to manage the state of your listeners:
chatsCollection.listen<Chat>(
results: (Map<String, Chat> chats) async {
state.chats = chats;
refresh();
},
);
// Actions over the reference
chatsCollection.pause();
chatsCollection.resume();
await chatsCollection.cancel();
// General-scope actions
FirestoreManager().pauseAll();
FirestoreManager().resumeAll();
await FirestoreManager().cancelAll();These methods allow you to control the behavior of your Firestore queries and listeners at a broad level, but they lack the specificity needed for fine-grained management.
The above example utilizes a general scope (a singleton), but we recommend using FirestoreViewModel scopes for better management:
final viewModel = FirestoreViewModel();
chatsCollection.listen<Chat>(
viewModel: viewModel,
results: (Map<String, Chat> chats) async {
state.chats = chats;
refresh();
},
);
/// actions over the reference
chatsCollection.pause();
chatsCollection.resume();
await chatsCollection.cancel();
/// specific-scope actions
viewModel.pauseAll();
viewModel.resumeAll();
viewModel.cancelAll();Using FirestoreViewModel scopes allows for more granular control over Firestore queries and listeners. It enables you to encapsulate data management logic within specific parts of your app, improving modularity and efficiency. With FirestoreViewModel, you can manage queries and listeners at a more localized level, reducing the risk of conflicts and improving the organization of your app's data handling.