use of com.google.firebase.firestore.core.ListenerRegistrationImpl in project firebase-android-sdk by firebase.
the class DocumentReference method addSnapshotListenerInternal.
/**
* Internal helper method to create add a snapshot listener.
*
* <p>Will be Activity scoped if the activity parameter is non-{@code null}.
*
* @param userExecutor The executor to use to call the listener.
* @param options The options to use for this listen.
* @param activity Optional activity this listener is scoped to.
* @param userListener The user-supplied event listener that will be called with document
* snapshots.
* @return A registration object that can be used to remove the listener.
*/
private ListenerRegistration addSnapshotListenerInternal(Executor userExecutor, ListenOptions options, @Nullable Activity activity, EventListener<DocumentSnapshot> userListener) {
// Convert from ViewSnapshots to DocumentSnapshots.
EventListener<ViewSnapshot> viewListener = (snapshot, error) -> {
if (error != null) {
userListener.onEvent(null, error);
return;
}
Assert.hardAssert(snapshot != null, "Got event without value or error set");
Assert.hardAssert(snapshot.getDocuments().size() <= 1, "Too many documents returned on a document query");
Document document = snapshot.getDocuments().getDocument(key);
DocumentSnapshot documentSnapshot;
if (document != null) {
boolean hasPendingWrites = snapshot.getMutatedKeys().contains(document.getKey());
documentSnapshot = DocumentSnapshot.fromDocument(firestore, document, snapshot.isFromCache(), hasPendingWrites);
} else {
// We don't raise `hasPendingWrites` for deleted documents.
documentSnapshot = DocumentSnapshot.fromNoDocument(firestore, key, snapshot.isFromCache());
}
userListener.onEvent(documentSnapshot, null);
};
// Call the viewListener on the userExecutor.
AsyncEventListener<ViewSnapshot> asyncListener = new AsyncEventListener<>(userExecutor, viewListener);
com.google.firebase.firestore.core.Query query = asQuery();
QueryListener queryListener = firestore.getClient().listen(query, options, asyncListener);
return ActivityScope.bind(activity, new ListenerRegistrationImpl(firestore.getClient(), queryListener, asyncListener));
}
use of com.google.firebase.firestore.core.ListenerRegistrationImpl in project firebase-android-sdk by firebase.
the class Query method addSnapshotListenerInternal.
/**
* Internal helper method to create add a snapshot listener.
*
* <p>Will be Activity scoped if the activity parameter is non-{@code null}.
*
* @param executor The executor to use to call the listener.
* @param options The options to use for this listen.
* @param activity Optional activity this listener is scoped to.
* @param userListener The user-supplied event listener that will be called with the snapshots.
* @return A registration object that can be used to remove the listener.
*/
private ListenerRegistration addSnapshotListenerInternal(Executor executor, ListenOptions options, @Nullable Activity activity, EventListener<QuerySnapshot> userListener) {
validateHasExplicitOrderByForLimitToLast();
// Convert from ViewSnapshots to QuerySnapshots.
EventListener<ViewSnapshot> viewListener = (@Nullable ViewSnapshot snapshot, @Nullable FirebaseFirestoreException error) -> {
if (error != null) {
userListener.onEvent(null, error);
return;
}
hardAssert(snapshot != null, "Got event without value or error set");
QuerySnapshot querySnapshot = new QuerySnapshot(this, snapshot, firestore);
userListener.onEvent(querySnapshot, null);
};
// Call the viewListener on the userExecutor.
AsyncEventListener<ViewSnapshot> asyncListener = new AsyncEventListener<>(executor, viewListener);
QueryListener queryListener = firestore.getClient().listen(query, options, asyncListener);
return ActivityScope.bind(activity, new ListenerRegistrationImpl(firestore.getClient(), queryListener, asyncListener));
}
Aggregations