8000 docs(sample): normalize firestore sample's region tags (#453) · googleapis/java-firestore@b529245 · GitHub
[go: up one dir, main page]

Skip to content

Commit b529245

Browse files
authored
docs(sample): normalize firestore sample's region tags (#453)
1 parent 22f98a1 commit b529245

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed

samples/snippets/src/main/java/com/example/firestore/Quickstart.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import com.google.auth.oauth2.GoogleCredentials;
2121
import com.google.cloud.firestore.DocumentReference;
2222
// [START fs_include_dependencies]
23+
// [START firestore_setup_dependencies]
2324
import com.google.cloud.firestore.Firestore;
2425
import com.google.cloud.firestore.FirestoreOptions;
26+
// [END firestore_setup_dependencies]
2527
// [END fs_include_dependencies]
2628
import com.google.cloud.firestore.QueryDocumentSnapshot;
2729
import com.google.cloud.firestore.QuerySnapshot;
@@ -44,19 +46,23 @@ public class Quickstart {
4446
*/
4547
public Quickstart() {
4648
// [START fs_initialize]
49+
// [START firestore_setup_client_create]
4750
Firestore db = FirestoreOptions.getDefaultInstance().getService();
51+
// [END firestore_setup_client_create]
4852
// [END fs_initialize]
4953
this.db = db;
5054
}
5155

5256
public Quickstart(String projectId) throws Exception {
5357
// [START fs_initialize_project_id]
58+
// [START firestore_setup_client_create_with_project_id]
5459
FirestoreOptions firestoreOptions =
5560
FirestoreOptions.getDefaultInstance().toBuilder()
5661
.setProjectId(projectId)
5762
.setCredentials(GoogleCredentials.getApplicationDefault())
5863
.build();
5964
Firestore db = firestoreOptions.getService();
65+
// [END firestore_setup_client_create_with_project_id]
6066
// [END fs_initialize_project_id]
6167
this.db = db;
6268
}
@@ -74,6 +80,7 @@ void addDocument(String docName) throws Exception {
7480
switch (docName) {
7581
case "alovelace": {
7682
// [START fs_add_data_1]
83+
// [START firestore_setup_dataset_pt1]
7784
DocumentReference docRef = db.collection("users").document("alovelace");
7885
// Add document data with id "alovelace" using a hashmap
7986
Map<String, Object> data = new HashMap<>();
@@ -85,11 +92,13 @@ void addDocument(String docName) throws Exception {
8592
// ...
8693
// result.get() blocks on response
8794
System.out.println("Update time : " + result.get().getUpdateTime());
95+
// [END firestore_setup_dataset_pt1]
8896
// [END fs_add_data_1]
8997
break;
9098
}
9199
case "aturing": {
92100
// [START fs_add_data_2]
101+
// [START firestore_setup_dataset_pt2]
93102
DocumentReference docRef = db.collection("users").document("aturing");
94103
// Add document data with an additional field ("middle")
95104
Map<String, Object> data = new HashMap<>();
@@ -100,6 +109,7 @@ void addDocument(String docName) throws Exception {
100109

101110
ApiFuture<WriteResult> result = docRef.set(data);
102111
System.out.println("Update time : " + result.get().getUpdateTime());
112+
// [END firestore_setup_dataset_pt2]
103113
// [END fs_add_data_2]
104114
break;
105115
}

samples/snippets/src/main/java/com/example/firestore/snippets/ListenDataSnippets.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Map<String, Object> listenToDocument() throws Exception {
5454
final SettableApiFuture<Map<String, Object>> future = SettableApiFuture.create();
5555

5656
// [START listen_to_document]
57+
// [START firestore_listen_document]
5758
DocumentReference docRef = db.collection("cities").document("SF");
5859
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
5960
@Override
@@ -76,6 +77,7 @@ public void onEvent(@Nullable DocumentSnapshot snapshot,
7677
// [END_EXCLUDE]
7778
}
7879
});
80+
// [END firestore_listen_document]
7981
// [END listen_to_document]
8082

8183
return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
@@ -88,6 +90,7 @@ List<String> listenForMultiple() throws Exception {
8890
final SettableApiFuture<List<String>> future = SettableApiFuture.create();
8991

9092
// [START listen_to_multiple]
93+
// [START firestore_listen_query_snapshots]
9194
db.collection("cities")
9295
.whereEqualTo("state", "CA")
9396
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@@ -113,6 +116,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
113116
// [END_EXCLUDE]
114117
}
115118
});
119+
// [END firestore_listen_query_snapshots]
116120
// [END listen_to_multiple]
117121

118122
return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
@@ -125,6 +129,7 @@ List<DocumentChange> listenForChanges() throws Exception {
125129
SettableApiFuture<List<DocumentChange>> future = SettableApiFuture.create();
126130

127131
// [START listen_for_changes]
132+
// [START firestore_listen_query_changes]
128133
db.collection("cities")
129134
.whereEqualTo("state", "CA")
130135
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@@ -158,6 +163,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
158163
// [END_EXCLUDE]
159164
}
160165
});
166+
// [END firestore_listen_query_changes]
161167
// [END listen_for_changes]
162168

163169
return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
@@ -168,6 +174,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
168174
*/
169175
void detachListener() {
170176
// [START detach_errors]
177+
// [START firestore_listen_detach]
171178
Query query = db.collection("cities");
172179
ListenerRegistration registration = query.addSnapshotListener(
173180
new EventListener<QuerySnapshot>() {
@@ -184,6 +191,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
184191

185192
// Stop listening to changes
186193
registration.remove();
194+
// [END firestore_listen_detach]
187195
// [END detach_errors]
188196
}
189197

@@ -192,6 +200,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
192200
*/
193201
void listenErrors() {
194202
// [START listen_errors]
203+
// [START firestore_listen_handle_error]
195204
db.collection("cities")
196205
.addSnapshotListener(new EventListener<QuerySnapshot>() {
197206
@Override
@@ -209,6 +218,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
209218
}
210219
}
211220
});
221+
// [END firestore_listen_handle_error]
212222
// [END listen_errors]
213223
}
214224

0 commit comments

Comments
 (0)
0