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

Commit e9d21d5

Browse files
#595 Creating Observables from Firestore Documents/Collections (doc)
1 parent f697243 commit e9d21d5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/FIRESTORE.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ citiesCollection.get().then(querySnapshot => {
4444
});
4545
```
4646

47+
### `collection().onSnapshot()`
48+
To listen to changes in a certain collection, you can register a callback function that gets invoked every time data is changed.
49+
50+
To unsubscribe the listener, just invoke the function that was returned when attaching the listener.
51+
52+
> NOTE: Due to a bug in the iOS Firestore SDK 'unsubscribe' does not currently work (plugin version 5.0.0), so the plugin is silently ignoring further updates after unsubscribing. This should not hurt your usage in any way though.
53+
54+
```typescript
55+
const citiesCollection = firebase.firestore().collection("cities");
56+
57+
const unsubscribe = citiesCollection.onSnapshot((snapshot: firestore.QuerySnapshot) => {
58+
snapshot.forEach(city => console.log(city.data()));
59+
});
60+
61+
// then after a while, to detach the listener:
62+
unsubscribe();
63+
```
64+
65+
> 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).
66+
4767
### `collection.doc()`
4868
As mentioned, a document lives inside a collection and contains the actual data:
4969

@@ -89,6 +109,8 @@ const unsubscribe = sanFranciscoDocument.onSnapshot(doc => {
89109
unsubscribe();
90110
```
91111

112+
> 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#L175-L185).
113+
92114
### `collection.add()`
93115
If you want to add a document with an auto-generated ID, use `add` on a *collection*:
94116

0 commit comments

Comments
 (0)