Berry Blue
Berry Blue

Reputation: 16482

How do you listen for changes in a Firestore collection based on a query?

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.

Upvotes: 0

Views: 375

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50830

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

Upvotes: 1

Related Questions