void ReadDataViewChangesBetweenSnapshots()

in firestore/android/FirestoreSnippetsCpp/app/src/main/cpp/snippets.cpp [730:766]


void ReadDataViewChangesBetweenSnapshots(firebase::firestore::Firestore* db) {
  using firebase::firestore::DocumentChange;
  using firebase::firestore::Error;
  using firebase::firestore::FieldValue;
  using firebase::firestore::QuerySnapshot;

  // It is often useful to see the actual changes to query results between query
  // snapshots, instead of simply using the entire query snapshot. For example,
  // you may want to maintain a cache as individual documents are added,
  // removed, and modified.
  // [START listen_diffs]
  db->Collection("cities")
      .WhereEqualTo("state", FieldValue::String("CA"))
      .AddSnapshotListener([](const QuerySnapshot& snapshot, Error error, const std::string& errorMsg) {
        if (error == Error::kErrorOk) {
          for (const DocumentChange& dc : snapshot.DocumentChanges()) {
            switch (dc.type()) {
              case DocumentChange::Type::kAdded:
                std::cout << "New city: "
                          << dc.document().Get("name").string_value() << std::endl;
                break;
              case DocumentChange::Type::kModified:
                std::cout << "Modified city: "
                          << dc.document().Get("name").string_value() << std::endl;
                break;
              case DocumentChange::Type::kRemoved:
                std::cout << "Removed city: "
                          << dc.document().Get("name").string_value() << std::endl;
                break;
            }
          }
        } else {
          std::cout << "Listen failed: " << error << std::endl;
        }
      });
  // [END listen_diffs]
}