Skip to content

Commit 21e248e

Browse files
docs(firestore-counter): use POSTINSTALL.md for client sample
1 parent 42ccfbf commit 21e248e

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

firestore-counter/POSTINSTALL.md

+53-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ match /databases/{database}/documents/pages/{page} {
2020
```
2121

2222

23-
#### Specify a document path and increment value in your web app
23+
#### Client samples for incrementing counter and retrieving its value
24+
25+
##### Web Client
26+
2427

2528
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.
2629

@@ -60,6 +63,55 @@ match /databases/{database}/documents/pages/{page} {
6063
</html>
6164
```
6265

66+
##### iOS Client
67+
68+
1. Ensure your Swift app already has Firebase [initialized](https://firebase.google.com/docs/ios/setup).
69+
2. Copy and paste the sample [code](https://github.com/firebase/extensions/blob/next/firestore-counter/clients/ios/Sources/FirestoreCounter/FirestoreCounter.swift) and create this file `FirestoreShardedCounter.swift` in the relevant directory you wish to use the `FirestoreShardedCounter` instance.
70+
71+
```swift
72+
import UIKit
73+
import FirestoreCounter
74+
import FirebaseFirestore
75+
76+
class ViewController: UIViewController {
77+
// somewhere in your app code initialize Firestore instance
78+
var db = Firestore.firestore()
79+
// create reference to the collection and the document you wish to use
80+
var doc = db.collection("pages").document("hello-world")
81+
// initialize FirestoreShardedCounter with the document and the property which will hold the counter value
82+
var controller = FirestoreShardCounter(docRef: doc, field: "visits")
83+
84+
override func viewDidLoad() {
85+
super.viewDidLoad()
86+
// event listener which returns total amount
87+
controller.onSnapshot { (value, error) in
88+
if let error = error {
89+
// handle error
90+
} else if let value = value {
91+
// 'value' param is total amount of pages visits
92+
}
93+
}
94+
}
95+
96+
@IBAction func getLatest(_ sender: Any) {
97+
// get current total
98+
controller.get() { (value, error) in
99+
if let error = error {
100+
// handle error
101+
} else if let value = value {
102+
// 'value' param is total amount of pages visits
103+
}
104+
}
105+
}
106+
107+
@IBAction func incrementer(_ sender: Any) {
108+
// to increment counter
109+
controller.incrementBy(val: Double(1))
110+
}
111+
}
112+
113+
```
114+
63115

64116
#### Upgrading from v0.1.3 and earlier
65117

firestore-counter/clients/ios/README.md

-47
This file was deleted.

0 commit comments

Comments
 (0)