use of com.google.cloud.firestore.FirestoreException in project java-docs-samples by GoogleCloudPlatform.
the class ListenDataSnippets method detachListener.
/**
* Demonstrate how to detach an event listener.
*/
void detachListener() {
// [START detach_errors]
Query query = db.collection("cities");
ListenerRegistration registration = query.addSnapshotListener(new EventListener<QuerySnapshot>() {
// [START_EXCLUDE]
@Override
public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirestoreException e) {
}
});
// ...
// Stop listening to changes
registration.remove();
// [END detach_errors]
}
use of com.google.cloud.firestore.FirestoreException in project java-docs-samples by GoogleCloudPlatform.
the class ListenDataSnippets method listenToDocument.
/**
* Listen to a single document, returning data after the first snapshot.
*/
Map<String, Object> listenToDocument() throws Exception {
final SettableApiFuture<Map<String, Object>> future = SettableApiFuture.create();
// [START listen_to_document]
DocumentReference docRef = db.collection("cities").document("SF");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirestoreException e) {
if (e != null) {
System.err.println("Listen failed: " + e);
return;
}
if (snapshot != null && snapshot.exists()) {
System.out.println("Current data: " + snapshot.getData());
} else {
System.out.print("Current data: null");
}
// [START_EXCLUDE silent]
if (!future.isDone()) {
future.set(snapshot.getData());
}
// [END_EXCLUDE]
}
});
return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
Aggregations