Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit f839d75

Browse files
Merge pull request #1154 from acrollet/master
Allow retrieving metadata with queries
2 parents f2eb65d + 26085b3 commit f839d75

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

docs/FIRESTORE.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ unsubscribe();
6464

6565
> Using **Observables**? [Check the example in the demo app](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/f6972433dea48bf1d342a6e4ef7f955dff341837/demo-ng/app/item/items.component.ts#L187-L198).
6666
67+
#### Determine if results are from cache or not
68+
If persistence is enabled, query results will first be returned from the local cache if available, and then from network. To determine if a document has been returned from the cache or not, pass the includeMetadataChanges parameter and inspect the metadata:
69+
70+
```typescript
71+
const citiesCollection = firebase.firestore().collection("cities");
72+
73+
const unsubscribe = citiesCollection.onSnapshot(({ includeMetadataChanges: true }, snapshot: firestore.QuerySnapshot) => {
74+
snapshot.forEach(city => console.log(city.data()));
75+
76+
const source = snapshot.metadata.fromCache ? "local cache" : "server";
77+
console.log("Data came from " + source);
78+
});
79+
80+
// then after a while, to detach the listener:
81+
unsubscribe();
82+
```
83+
6784
### `collection.doc()`
6885
As mentioned, a document lives inside a collection and contains the actual data:
6986

src/firebase.android.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,7 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22292229
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
22302230
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, collectionRef),
22312231
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, collectionRef),
2232-
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(collectionRef, callback),
2232+
onSnapshot: (optionsOrCallback: any, callback?: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(collectionRef, optionsOrCallback, callback),
22332233
startAfter: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshot, collectionRef),
22342234
startAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAt(collectionPath, snapshot, collectionRef),
22352235
endAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endAt(collectionPath, snapshot, collectionRef),
@@ -2255,8 +2255,15 @@ firebase.firestore.onDocumentSnapshot = (docRef: com.google.firebase.firestore.D
22552255
return () => listener.remove();
22562256
};
22572257

2258-
firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore.CollectionReference, callback: (snapshot: QuerySnapshot) => void): () => void => {
2259-
const listener = colRef.addSnapshotListener(new com.google.firebase.firestore.EventListener({
2258+
firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore.CollectionReference, optionsOrCallback: any, callback: (snapshot: QuerySnapshot) => void): () => void => {
2259+
let options = com.google.firebase.firestore.MetadataChanges.EXCLUDE;
2260+
if ((typeof optionsOrCallback) === 'function') {
2261+
callback = optionsOrCallback;
2262+
} else if (optionsOrCallback.includeMetadataChanges) {
2263+
options = com.google.firebase.firestore.MetadataChanges.INCLUDE;
2264+
}
2265+
2266+
const listener = colRef.addSnapshotListener(options, new com.google.firebase.firestore.EventListener({
22602267
onEvent: ((snapshot: com.google.firebase.firestore.QuerySnapshot, exception: com.google.firebase.firestore.FirebaseFirestoreException) => {
22612268
if (exception !== null) {
22622269
console.error('onCollectionSnapshot error code: ' + exception.getCode());
@@ -2558,7 +2565,7 @@ firebase.firestore._getQuery = (collectionPath: string, query: com.google.fireba
25582565
where: (fp: string, os: firestore.WhereFilterOp, v: any): firestore.Query => firebase.firestore.where(collectionPath, fp, os, v, query),
25592566
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
25602567
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query),
2561-
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(query, callback),
2568+
onSnapshot: (optionsOrCallback: any, callback?: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(query, optionsOrCallback, callback),
25622569
startAfter: (snapshot: DocumentSnapshot) => firebase.firestore.startAfter(collectionPath, snapshot, query),
25632570
startAt: (snapshot: DocumentSnapshot) => firebase.firestore.startAt(collectionPath, snapshot, query),
25642571
endAt: (snapshot: DocumentSnapshot) => firebase.firestore.endAt(collectionPath, snapshot, query),
@@ -2670,6 +2677,10 @@ export class QuerySnapshot implements firestore.QuerySnapshot {
26702677
constructor(private snapshot: com.google.firebase.firestore.QuerySnapshot) {
26712678
}
26722679

2680+
metadata = {
2681+
fromCache: this.snapshot.getMetadata().isFromCache(),
2682+
};
2683+
26732684
get docs(): firestore.QueryDocumentSnapshot[] {
26742685
const getSnapshots = () => {
26752686
const docSnapshots: firestore.QueryDocumentSnapshot[] = [];

src/firebase.ios.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
18791879
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
18801880
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, fIRCollectionReference),
18811881
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, fIRCollectionReference),
1882-
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(fIRCollectionReference, callback),
1882+
onSnapshot: (optionsOrCallback: any, callback?: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(fIRCollectionReference, optionsOrCallback, callback),
18831883
startAfter: (document: DocumentSnapshot) => firebase.firestore.startAfter(collectionPath, document, fIRCollectionReference),
18841884
startAt: (document: DocumentSnapshot) => firebase.firestore.startAt(collectionPath, document, fIRCollectionReference),
18851885
endAt: (document: DocumentSnapshot) => firebase.firestore.endAt(collectionPath, document, fIRCollectionReference),
@@ -1911,8 +1911,14 @@ firebase.firestore.onDocumentSnapshot = (docRef: FIRDocumentReference, callback:
19111911
}
19121912
};
19131913

1914-
firebase.firestore.onCollectionSnapshot = (colRef: FIRCollectionReference, callback: (snapshot: QuerySnapshot) => void): () => void => {
1915-
const listener = colRef.addSnapshotListener((snapshot: FIRQuerySnapshot, error: NSError) => {
1914+
firebase.firestore.onCollectionSnapshot = (colRef: FIRCollectionReference, optionsOrCallback: any, callback: (snapshot: QuerySnapshot) => void): () => void => {
1915+
var includeMetadataChanges = false;
1916+
if ((typeof optionsOrCallback) === 'function') {
1917+
callback = optionsOrCallback;
1918+
} else if (optionsOrCallback.includeMetadataChanges === true) {
1919+
includeMetadataChanges = true;
1920+
}
1921+
const listener = colRef.addSnapshotListenerWithIncludeMetadataChangesListener(includeMetadataChanges, (snapshot: FIRQuerySnapshot, error: NSError) => {
19161922
if (error || !snapshot) {
19171923
return;
19181924
}
@@ -2219,7 +2225,7 @@ firebase.firestore._getQuery = (collectionPath: string, query: FIRQuery): firest
22192225
where: (fp: string, os: firestore.WhereFilterOp, v: any): firestore.Query => firebase.firestore.where(collectionPath, fp, os, v, query),
22202226
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
22212227
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query),
2222-
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(query, callback),
2228+
onSnapshot: (optionsOrCallback: any, callback?: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(query, optionsOrCallback, callback),
22232229
startAfter: (document: DocumentSnapshot) => firebase.firestore.startAfter(collectionPath, document, query),
22242230
startAt: (document: DocumentSnapshot) => firebase.firestore.startAt(collectionPath, document, query),
22252231
endAt: (document: DocumentSnapshot) => firebase.firestore.endAt(collectionPath, document, query),
@@ -2372,6 +2378,8 @@ export class QuerySnapshot implements firestore.QuerySnapshot {
23722378
constructor(private snapshot: FIRQuerySnapshot) {
23732379
}
23742380

2381+
metadata = this.snapshot.metadata;
2382+
23752383
get docs(): firestore.QueryDocumentSnapshot[] {
23762384
const getSnapshots = () => {
23772385
const docSnapshots: firestore.QueryDocumentSnapshot[] = [];

0 commit comments

Comments
 (0)