com.google.firebase.firestore.MetadataChanges Java Examples
The following examples show how to use
com.google.firebase.firestore.MetadataChanges.
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 |
public void offlineListen(FirebaseFirestore db) { // [START offline_listen] db.collection("cities").whereEqualTo("state", "CA") .addSnapshotListener(MetadataChanges.INCLUDE, new EventListener<QuerySnapshot>() { @Override public void onEvent(@Nullable QuerySnapshot querySnapshot, @Nullable FirebaseFirestoreException e) { if (e != null) { Log.w(TAG, "Listen error", e); return; } for (DocumentChange change : querySnapshot.getDocumentChanges()) { if (change.getType() == Type.ADDED) { Log.d(TAG, "New city:" + change.getDocument().getData()); } String source = querySnapshot.getMetadata().isFromCache() ? "local cache" : "server"; Log.d(TAG, "Data fetched from " + source); } } }); // [END offline_listen] }
Example #2
Source File: IntegrationTestUtil.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
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 #3
Source File: QueryListenerTest.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
@Test public void testDoesNotRaiseEventsForMetadataChangesUnlessSpecified() { List<ViewSnapshot> filteredAccum = new ArrayList<>(); List<ViewSnapshot> fullAccum = new ArrayList<>(); Query query = Query.atPath(path("rooms")); Document doc1 = doc("rooms/eros", 1, map("name", "eros")); Document doc2 = doc("rooms/hades", 2, map("name", "hades")); ListenOptions options1 = new ListenOptions(); ListenOptions options2 = new ListenOptions(); options2.includeQueryMetadataChanges = true; options2.includeDocumentMetadataChanges = true; QueryListener filteredListener = queryListener(query, options1, filteredAccum); QueryListener fullListener = queryListener(query, options2, fullAccum); View view = new View(query, DocumentKey.emptyKeySet()); ViewSnapshot snap1 = applyChanges(view, doc1); TargetChange ackTarget = ackTarget(doc1); ViewSnapshot snap2 = view.applyChanges(view.computeDocChanges(docUpdates()), ackTarget).getSnapshot(); ViewSnapshot snap3 = applyChanges(view, doc2); filteredListener.onViewSnapshot(snap1); // local event filteredListener.onViewSnapshot(snap2); // no event filteredListener.onViewSnapshot(snap3); // doc2 update fullListener.onViewSnapshot(snap1); // local event fullListener.onViewSnapshot(snap2); // no event fullListener.onViewSnapshot(snap3); // doc2 update assertEquals( asList( applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), applyExpectedMetadata(snap3, MetadataChanges.EXCLUDE)), filteredAccum); assertEquals(asList(snap1, snap2, snap3), fullAccum); }
Example #4
Source File: QueryListenerTest.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
@Test public void testRaisesDocumentMetadataEventsOnlyWhenSpecified() { List<ViewSnapshot> filteredAccum = new ArrayList<>(); List<ViewSnapshot> fullAccum = new ArrayList<>(); Query query = Query.atPath(path("rooms")); Document doc1 = doc("rooms/eros", 1, map("name", "eros")); Document doc1Prime = doc("rooms/eros", 1, map("name", "eros"), Document.DocumentState.LOCAL_MUTATIONS); Document doc2 = doc("rooms/hades", 2, map("name", "hades")); Document doc3 = doc("rooms/other", 3, map("name", "other")); ListenOptions options1 = new ListenOptions(); ListenOptions options2 = new ListenOptions(); options2.includeDocumentMetadataChanges = true; QueryListener filteredListener = queryListener(query, options1, filteredAccum); QueryListener fullListener = queryListener(query, options2, fullAccum); View view = new View(query, DocumentKey.emptyKeySet()); ViewSnapshot snap1 = applyChanges(view, doc1, doc2); ViewSnapshot snap2 = applyChanges(view, doc1Prime); ViewSnapshot snap3 = applyChanges(view, doc3); filteredListener.onViewSnapshot(snap1); filteredListener.onViewSnapshot(snap2); filteredListener.onViewSnapshot(snap3); fullListener.onViewSnapshot(snap1); fullListener.onViewSnapshot(snap2); fullListener.onViewSnapshot(snap3); assertEquals( asList( applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), applyExpectedMetadata(snap3, MetadataChanges.EXCLUDE)), filteredAccum); // Second listener should receive doc1prime as added document not modified assertEquals(asList(snap1, snap2, snap3), fullAccum); }
Example #5
Source File: QueryListenerTest.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
@Test public void testMetadataOnlyDocumentChangesAreFilteredOut() { List<ViewSnapshot> filteredAccum = new ArrayList<>(); Query query = Query.atPath(path("rooms")); Document doc1 = doc("rooms/eros", 1, map("name", "eros")); Document doc1Prime = doc("rooms/eros", 1, map("name", "eros"), Document.DocumentState.LOCAL_MUTATIONS); Document doc2 = doc("rooms/hades", 2, map("name", "hades")); Document doc3 = doc("rooms/other", 3, map("name", "other")); ListenOptions options = new ListenOptions(); options.includeDocumentMetadataChanges = false; QueryListener filteredListener = queryListener(query, options, filteredAccum); View view = new View(query, DocumentKey.emptyKeySet()); ViewSnapshot snap1 = applyChanges(view, doc1, doc2); ViewSnapshot snap2 = applyChanges(view, doc1Prime, doc3); filteredListener.onViewSnapshot(snap1); filteredListener.onViewSnapshot(snap2); DocumentViewChange change3 = DocumentViewChange.create(Type.ADDED, doc3); ViewSnapshot expectedSnapshot2 = new ViewSnapshot( snap2.getQuery(), snap2.getDocuments(), snap1.getDocuments(), asList(change3), snap2.isFromCache(), snap2.getMutatedKeys(), snap2.didSyncStateChange(), /* excludesMetadataChanges= */ true); assertEquals( asList(applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), expectedSnapshot2), filteredAccum); }
Example #6
Source File: QueryListenerTest.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
private ViewSnapshot applyExpectedMetadata(ViewSnapshot snap, MetadataChanges metadata) { return new ViewSnapshot( snap.getQuery(), snap.getDocuments(), snap.getOldDocuments(), snap.getChanges(), snap.isFromCache(), snap.getMutatedKeys(), snap.didSyncStateChange(), MetadataChanges.EXCLUDE.equals(metadata)); }
Example #7
Source File: DocSnippets.java From snippets-android with Apache License 2.0 | 5 votes |
public void listenWithMetadata() { // [START listen_with_metadata] // Listen for metadata changes to the document. DocumentReference docRef = db.collection("cities").document("SF"); docRef.addSnapshotListener(MetadataChanges.INCLUDE, new EventListener<DocumentSnapshot>() { @Override public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirebaseFirestoreException e) { // ... } }); // [END listen_with_metadata] }
Example #8
Source File: FirestoreArray.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
/** * @param changes metadata options for the query listen. * @see #FirestoreArray(Query, SnapshotParser) */ public FirestoreArray(@NonNull Query query, @NonNull MetadataChanges changes, @NonNull SnapshotParser<T> parser) { super(parser); mQuery = query; mMetadataChanges = changes; }
Example #9
Source File: FirestoreRecyclerOptions.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
/** * Set the query to use (with options) and provide a model class to which each snapshot will * be converted. * <p> * Do not call this method after calling {@link #setSnapshotArray(ObservableSnapshotArray)}. */ @NonNull public Builder<T> setQuery(@NonNull Query query, @NonNull MetadataChanges changes, @NonNull Class<T> modelClass) { return setQuery(query, changes, new ClassSnapshotParser<>(modelClass)); }
Example #10
Source File: FirestoreRecyclerOptions.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
/** * Set the query to use (with options) and provide a custom {@link SnapshotParser}. * <p> * Do not call this method after calling {@link #setSnapshotArray(ObservableSnapshotArray)}. */ @NonNull public Builder<T> setQuery(@NonNull Query query, @NonNull MetadataChanges changes, @NonNull SnapshotParser<T> parser) { assertNull(mSnapshots, ERR_SNAPSHOTS_SET); mSnapshots = new FirestoreArray<>(query, changes, parser); return this; }
Example #11
Source File: QueryListenerTest.java From firebase-android-sdk with Apache License 2.0 | 4 votes |
@Test public void testRaisesQueryMetadataEventsOnlyWhenHasPendingWritesOnTheQueryChanges() { List<ViewSnapshot> fullAccum = new ArrayList<>(); Query query = Query.atPath(path("rooms")); Document doc1 = doc("rooms/eros", 1, map("name", "eros"), Document.DocumentState.LOCAL_MUTATIONS); Document doc2 = doc("rooms/hades", 2, map("name", "hades"), Document.DocumentState.LOCAL_MUTATIONS); Document doc1Prime = doc("rooms/eros", 1, map("name", "eros")); Document doc2Prime = doc("rooms/hades", 2, map("name", "hades")); Document doc3 = doc("rooms/other", 3, map("name", "other")); ListenOptions options = new ListenOptions(); options.includeQueryMetadataChanges = true; QueryListener fullListener = queryListener(query, options, fullAccum); View view = new View(query, DocumentKey.emptyKeySet()); ViewSnapshot snap1 = applyChanges(view, doc1, doc2); ViewSnapshot snap2 = applyChanges(view, doc1Prime); ViewSnapshot snap3 = applyChanges(view, doc3); ViewSnapshot snap4 = applyChanges(view, doc2Prime); fullListener.onViewSnapshot(snap1); fullListener.onViewSnapshot(snap2); // Emits no events fullListener.onViewSnapshot(snap3); fullListener.onViewSnapshot(snap4); // Metadata change event ViewSnapshot expectedSnapshot4 = new ViewSnapshot( snap4.getQuery(), snap4.getDocuments(), snap3.getDocuments(), asList(), snap4.isFromCache(), snap4.getMutatedKeys(), snap4.didSyncStateChange(), /* excludeMetadataChanges= */ true); // This test excludes document metadata changes assertEquals( asList( applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), applyExpectedMetadata(snap3, MetadataChanges.EXCLUDE), expectedSnapshot4), fullAccum); }
Example #12
Source File: SolutionRateLimiting.java From snippets-android with Apache License 2.0 | 4 votes |
public void startUpdates() { String deviceId = "my-device-id"; final DocumentReference reference = db.collection("readings").document(deviceId); // Listen to the document, including metadata changes so we get notified // when writes have propagated to the server. mRegistration = reference.addSnapshotListener(MetadataChanges.INCLUDE, new EventListener<DocumentSnapshot>() { @Override public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) { if (e != null) { Log.w(TAG, "onEvent:error", e); } if (documentSnapshot != null && !documentSnapshot.getMetadata().hasPendingWrites()) { Log.d(TAG, "Got server snapshot."); mReadyForUpdate = true; } } }); // On a regular interval, attempt to update the document. Only perform the update // if we have gotten a snapshot from the server since the last update, which means // we are ready to update again. mHandler.postDelayed(new Runnable() { @Override public void run() { if (mReadyForUpdate) { Log.d(TAG, "Updating sensor data"); // Update the document Map<String, Object> updates = new HashMap<>(); updates.put("temperature", getCurrentTemperature()); updates.put("timestamp", FieldValue.serverTimestamp()); reference.set(updates, SetOptions.merge()); // Mark 'ready for update' as false until we get the next snapshot from the server mReadyForUpdate = false; } else { Log.d(TAG, "Not ready for update"); } // Schedule the next update mHandler.postDelayed(this, UPDATE_INTERVAL_MS); } }, UPDATE_INTERVAL_MS); }
Example #13
Source File: FirestoreRecyclerOptions.java From FirebaseUI-Android with Apache License 2.0 | 4 votes |
/** * Calls {@link #setQuery(Query, MetadataChanges, Class)} with metadata changes excluded. */ @NonNull public Builder<T> setQuery(@NonNull Query query, @NonNull SnapshotParser<T> parser) { return setQuery(query, MetadataChanges.EXCLUDE, parser); }
Example #14
Source File: FirestoreRecyclerOptions.java From FirebaseUI-Android with Apache License 2.0 | 4 votes |
/** * Calls {@link #setQuery(Query, MetadataChanges, Class)} with metadata changes excluded. */ @NonNull public Builder<T> setQuery(@NonNull Query query, @NonNull Class<T> modelClass) { return setQuery(query, MetadataChanges.EXCLUDE, modelClass); }
Example #15
Source File: FirestoreArray.java From FirebaseUI-Android with Apache License 2.0 | 2 votes |
/** * Create a new FirestoreArray. * * @param query query to listen to. * @param parser parser for DocumentSnapshots. * @see ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser) */ public FirestoreArray(@NonNull Query query, @NonNull SnapshotParser<T> parser) { this(query, MetadataChanges.EXCLUDE, parser); }