How do you listen for changes in a Firestore collection if a document is added or deleted that matches a query? This is for an iOS app in Objective-C.
CodePudding user response:
You can use addSnapshotListener
on your Query:
[[[self.db collectionWithPath:@"cities"] queryWhereField:@"state" isEqualTo:@"CA"]
addSnapshotListener:^(FIRQuerySnapshot *snapshot, NSError *error) {
if (snapshot == nil) {
NSLog(@"Error fetching documents: %@", error);
return;
}
NSMutableArray *cities = [NSMutableArray array];
for (FIRDocumentSnapshot *document in snapshot.documents) {
[cities addObject:document.data[@"name"]];
}
NSLog(@"Current cities in CA: %@", cities);
}];
This listener will to documents where state field is CA. You can read more about this in the documentation