Java Code Examples for com.google.firebase.firestore.ListenerRegistration#remove()

The following examples show how to use com.google.firebase.firestore.ListenerRegistration#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocSnippets.java    From snippets-android with Apache License 2.0 6 votes vote down vote up
public void detachListener() {
    // [START detach_listener]
    Query query = db.collection("cities");
    ListenerRegistration registration = query.addSnapshotListener(
            new EventListener<QuerySnapshot>() {
                // [START_EXCLUDE]
                @Override
                public void onEvent(@Nullable QuerySnapshot snapshots,
                                    @Nullable FirebaseFirestoreException e) {
                    // ...
                }
                // [END_EXCLUDE]
            });

    // ...

    // Stop listening to changes
    registration.remove();
    // [END detach_listener]
}
 
Example 2
Source File: FirestoreTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void setShouldTriggerListenerWithNewlySetData() throws Exception {
  FirebaseAuth auth = FirebaseAuth.getInstance();
  FirebaseFirestore firestore = FirebaseFirestore.getInstance();

  auth.signOut();
  Task<?> signInTask = auth.signInWithEmailAndPassword("[email protected]", "password");
  Tasks2.waitForSuccess(signInTask);

  DocumentReference doc = firestore.collection("restaurants").document(TestId.create());
  SnapshotListener listener = new SnapshotListener();
  ListenerRegistration registration = doc.addSnapshotListener(listener);

  try {
    HashMap<String, Object> data = new HashMap<>();
    data.put("location", "Google NYC");

    Task<?> setTask = doc.set(new HashMap<>(data));
    Task<DocumentSnapshot> snapshotTask = listener.toTask();
    Tasks2.waitForSuccess(setTask);
    Tasks2.waitForSuccess(snapshotTask);

    DocumentSnapshot result = snapshotTask.getResult();
    assertThat(result.getData()).isEqualTo(data);
  } finally {
    registration.remove();
    Tasks2.waitBestEffort(doc.delete());
  }
}
 
Example 3
Source File: IntegrationTestUtil.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
private static void primeBackend() {
  if (!backendPrimed) {
    backendPrimed = true;
    TaskCompletionSource<Void> watchInitialized = new TaskCompletionSource<>();
    TaskCompletionSource<Void> watchUpdateReceived = new TaskCompletionSource<>();
    DocumentReference docRef = testDocument();
    ListenerRegistration listenerRegistration =
        docRef.addSnapshotListener(
            (snapshot, error) -> {
              assertNull(error);
              if ("done".equals(snapshot.get("value"))) {
                watchUpdateReceived.setResult(null);
              } else {
                watchInitialized.setResult(null);
              }
            });

    // Wait for watch to initialize and deliver first event.
    waitFor(watchInitialized.getTask());

    // Use a transaction to perform a write without triggering any local events.
    docRef
        .getFirestore()
        .runTransaction(
            transaction -> {
              transaction.set(docRef, map("value", "done"));
              return null;
            });

    // Wait to see the write on the watch stream.
    waitFor(watchUpdateReceived.getTask(), PRIMING_TIMEOUT_MS);

    listenerRegistration.remove();
  }
}
 
Example 4
Source File: IntegrationTestUtil.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
public static void waitForOnlineSnapshot(DocumentReference doc) {
  TaskCompletionSource<Void> done = new TaskCompletionSource<>();
  ListenerRegistration registration =
      doc.addSnapshotListener(
          MetadataChanges.INCLUDE,
          (snapshot, error) -> {
            assertNull(error);
            if (!snapshot.getMetadata().isFromCache()) {
              done.setResult(null);
            }
          });
  waitFor(done.getTask());
  registration.remove();
}
 
Example 5
Source File: FirestoreTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void updateShouldTriggerListenerWithUpdatedData() throws Exception {
  FirebaseAuth auth = FirebaseAuth.getInstance();
  FirebaseFirestore firestore = FirebaseFirestore.getInstance();

  auth.signOut();
  Task<?> signInTask = auth.signInWithEmailAndPassword("[email protected]", "password");
  Tasks2.waitForSuccess(signInTask);

  DocumentReference doc = firestore.collection("restaurants").document(TestId.create());
  try {
    HashMap<String, Object> originalData = new HashMap<>();
    originalData.put("location", "Google NYC");

    Task<?> setTask = doc.set(new HashMap<>(originalData));
    Tasks2.waitForSuccess(setTask);

    SnapshotListener listener = new SnapshotListener(2);
    ListenerRegistration registration = doc.addSnapshotListener(listener);

    try {
      HashMap<String, Object> updateData = new HashMap<>();
      updateData.put("priority", 5L);

      Task<?> updateTask = doc.update(new HashMap<>(updateData));
      Task<DocumentSnapshot> snapshotTask = listener.toTask();
      Tasks2.waitForSuccess(updateTask);
      Tasks2.waitForSuccess(snapshotTask);

      DocumentSnapshot result = snapshotTask.getResult();
      HashMap<String, Object> finalData = new HashMap<>();
      finalData.put("location", "Google NYC");
      finalData.put("priority", 5L);
      assertThat(result.getData()).isEqualTo(finalData);
    } finally {
      registration.remove();
    }
  } finally {
    Tasks2.waitBestEffort(doc.delete());
  }
}