Search in sources :

Example 1 with FirestoreException

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]
}
Also used : Query(com.google.cloud.firestore.Query) FirestoreException(com.google.cloud.firestore.FirestoreException) ListenerRegistration(com.google.cloud.firestore.ListenerRegistration) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 2 with FirestoreException

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);
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) FirestoreException(com.google.cloud.firestore.FirestoreException) Map(java.util.Map) DocumentReference(com.google.cloud.firestore.DocumentReference)

Aggregations

FirestoreException (com.google.cloud.firestore.FirestoreException)2 DocumentReference (com.google.cloud.firestore.DocumentReference)1 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)1 ListenerRegistration (com.google.cloud.firestore.ListenerRegistration)1 Query (com.google.cloud.firestore.Query)1 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)1 Map (java.util.Map)1