Skip to content

Commit

Permalink
docs(firestore-counter): android client example (firebase#541)
Browse files Browse the repository at this point in the history
* docs(firestore-counter): android client example

* docs(firestore-counter): update numbering

* docs(firestore-counter): suggestions from code review

Co-authored-by: Kevin Cheung <[email protected]>

* Update firestore-counter/POSTINSTALL.md

Co-authored-by: Kevin Cheung <[email protected]>
Co-authored-by: Jeff <[email protected]>
  • Loading branch information
3 people authored Mar 2, 2021
1 parent d16cf2d commit 2599cbe
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions firestore-counter/POSTINSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ match /databases/{database}/documents/pages/{page} {
}
```


#### Client samples for incrementing counter and retrieving its value

##### Web Client


1. Download and copy the [compiled client sample](https://github.com/firebase/extensions/blob/master/firestore-counter/clients/web/dist/sharded-counter.js) into your application project.

1. Use the client sample in your code to increment counters. The code snippet below shows an example of how to use it. To see the full reference implementation, refer to the sample's TypeScript [source code](https://github.com/firebase/extensions/blob/master/firestore-counter/clients/web/src/index.ts).
Expand Down Expand Up @@ -63,6 +61,55 @@ match /databases/{database}/documents/pages/{page} {
</html>
```

##### Android Client

1. Follow the steps in [Add Firebase to your Android project](https://firebase.google.com/docs/android/setup) to use Firebase in your app.

2. Copy the [sample code](https://github.com/firebase/extensions/blob/next/firestore-counter/clients/android/src/main/java/com/firebase/firestore/counter/FirestoreShardedCounter.java) to the directory in which you want to use the `FirestoreShardedCounter` instance.

```java
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.ListenerRegistration;


// somewhere in your app code initialize Firestore instance
FirebaseFirestore db = FirebaseFirestore.getInstance();
// create reference to the collection and the document you wish to use
DocumentReference doc = db.collection("pages").document("hello-world");
// initialize FirestoreShardedCounter with the document and the property which will hold the counter value
FirestoreShardedCounter visits = new FirestoreShardedCounter(doc, "visits");

// to increment counter
visits.incrementBy(1);

// listen for updates
EventListener<Double> snapshotListener = new EventListener<Double>(){
@Override
public void onEvent(@Nullable Double value, @Nullable FirebaseFirestoreException error) {
// 'value' param is total amount of pages visits
}
};
ListenerRegistration registration = visits.onSnapshot(snapshotListener);
// clean up event listeners once finished
registration.remove();

// make one time call to query total amount of visits
double totalVisits = visits.get();

// if you don't mind counter delays, you can listen to the document directly.
db.document("pages/hello-world").addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
// total page visits
double pageVisits = (double) value.get("visits");
}
});
```

##### iOS Client

1. Ensure your Swift app already has Firebase [initialized](https://firebase.google.com/docs/ios/setup).
Expand Down Expand Up @@ -112,7 +159,6 @@ class ViewController: UIViewController {

```


#### Upgrading from v0.1.3 and earlier

If you installed v0.1.3 or an earlier version of this extension, you set up a Cloud Scheduler job that either sent messages to the extension's Pub/Sub topic (`${param:EXT_INSTANCE_ID}`) or called the extension's controller function. Starting in v0.1.4, the controllerCore function (`${function:controllerCore.name}`) has a configurable schedule, so the manually-created Cloud Scheduler job is no longer required and will start to fail.
Expand All @@ -126,11 +172,11 @@ After you complete the post-installation configuration above, the process runs a

1. Your extension creates subcollections in all the documents that your app uses as counters.

1. The client sample writes to these subcollections to distribute the write load.
2. The client sample writes to these subcollections to distribute the write load.

1. The controllerCore function sums the subcollections' values into the single `visits` field (or whichever field you configured in your master document).
3. The controllerCore function sums the subcollections' values into the single `visits` field (or whichever field you configured in your master document).

1. After each summation, the extension deletes the subcollections, leaving only the count in the master document. This is the document field to which you should listen for the count.
4. After each summation, the extension deletes the subcollections, leaving only the count in the master document. This is the document field to which you should listen for the count.

### Monitoring

Expand Down

0 comments on commit 2599cbe

Please sign in to comment.