com.google.firebase.firestore.ListenerRegistration Java Examples

The following examples show how to use com.google.firebase.firestore.ListenerRegistration. 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: FirestoreDocumentOnSubscribe.java    From Track-My-Location with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void subscribe(ObservableEmitter<DocumentSnapshot> e) throws Exception {
    DocEventListener listener = new DocEventListener(e);
    ListenerRegistration registration = mDocumentReference.addSnapshotListener(listener);
    e.setDisposable(new Disposable() {
        boolean disposed = false;
        @Override
        public void dispose() {
            if (!isDisposed()) {
                registration.remove();
                listener.emitter = null;
                disposed = true;
            }
        }

        @Override
        public boolean isDisposed() {
            return disposed;
        }
    });
}
 
Example #2
Source File: FirestoreQueryOnSubscribe.java    From Track-My-Location with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void subscribe(FlowableEmitter<QuerySnapshot> e) throws Exception {
    QueryEventListener listener = new QueryEventListener(e);
    ListenerRegistration registration = mQuery.addSnapshotListener(listener);
    e.setDisposable(new Disposable() {
        boolean disposed = false;
        @Override
        public void dispose() {
            if (!isDisposed()) {
                registration.remove();
                listener.emitter = null;
                disposed = true;
            }
        }

        @Override
        public boolean isDisposed() {
            return disposed;
        }
    });
}
 
Example #3
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 #4
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 #5
Source File: ActivityScope.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/** Binds the given registration to the lifetime of the activity. */
public static ListenerRegistration bind(
    @Nullable Activity activity, ListenerRegistration registration) {
  if (activity != null) {
    if (activity instanceof FragmentActivity) {
      onFragmentActivityStopCallOnce((FragmentActivity) activity, registration::remove);
    } else {
      onActivityStopCallOnce(activity, registration::remove);
    }
  }
  return registration;
}
 
Example #6
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 #7
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 #8
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());
  }
}