in firestore/android/FirestoreSnippetsCpp/app/src/main/cpp/snippets.cpp [603:627]
void ReadDataListen(firebase::firestore::Firestore* db) {
using firebase::firestore::DocumentReference;
using firebase::firestore::DocumentSnapshot;
using firebase::firestore::Error;
// You can listen to a document with the AddSnapshotListener() method. An
// initial call using the callback you provide creates a document snapshot
// immediately with the current contents of the single document. Then, each
// time the contents change, another call updates the document snapshot.
// [START listen_document]
DocumentReference doc_ref = db->Collection("cities").Document("SF");
doc_ref.AddSnapshotListener(
[](const DocumentSnapshot& snapshot, Error error, const std::string& errorMsg) {
if (error == Error::kErrorOk) {
if (snapshot.exists()) {
std::cout << "Current data: " << snapshot << std::endl;
} else {
std::cout << "Current data: null" << std::endl;
}
} else {
std::cout << "Listen failed: " << error << std::endl;
}
});
// [END listen_document]
}