void ReadDataListenToMultipleDocumentsInCollection()

in firestore/android/FirestoreSnippetsCpp/app/src/main/cpp/snippets.cpp [698:727]


void ReadDataListenToMultipleDocumentsInCollection(
    firebase::firestore::Firestore* db) {
  using firebase::firestore::DocumentSnapshot;
  using firebase::firestore::Error;
  using firebase::firestore::FieldValue;
  using firebase::firestore::QuerySnapshot;

  // As with documents, you can use AddSnapshotListener() instead of Get() to
  // listen to the results of a query. This creates a query snapshot. For
  // example, to listen to the documents with state CA:
  // [START listen_multiple]
  db->Collection("cities")
      .WhereEqualTo("state", FieldValue::String("CA"))
      .AddSnapshotListener([](const QuerySnapshot& snapshot, Error error, const std::string& errorMsg) {
        if (error == Error::kErrorOk) {
          std::vector<std::string> cities;
          std::cout << "Current cities in CA: " << error << std::endl;
          for (const DocumentSnapshot& doc : snapshot.documents()) {
            cities.push_back(doc.Get("name").string_value());
            std::cout << "" << cities.back() << std::endl;
          }
        } else {
          std::cout << "Listen failed: " << error << std::endl;
        }
      });
  // [END listen_multiple]

  // The snapshot handler will receive a new query snapshot every time the query
  // results change (that is, when a document is added, removed, or modified).
}