|
36 | 36 | import com.google.firebase.firestore.ListenerRegistration;
|
37 | 37 | import com.google.firebase.firestore.MetadataChanges;
|
38 | 38 | import com.google.firebase.firestore.QuerySnapshot;
|
| 39 | +import com.google.firebase.firestore.WriteBatch; |
39 | 40 | import com.google.firebase.firestore.auth.User;
|
40 | 41 | import com.google.firebase.firestore.core.DatabaseInfo;
|
41 | 42 | import com.google.firebase.firestore.model.DatabaseId;
|
@@ -347,8 +348,27 @@ public static CollectionReference testCollectionWithDocs(Map<String, Map<String,
|
347 | 348 |
|
348 | 349 | public static void writeAllDocs(
|
349 | 350 | CollectionReference collection, Map<String, Map<String, Object>> docs) {
|
| 351 | + WriteBatch writeBatch = null; |
| 352 | + int writeBatchSize = 0; |
| 353 | + |
350 | 354 | for (Map.Entry<String, Map<String, Object>> doc : docs.entrySet()) {
|
351 |
| - waitFor(collection.document(doc.getKey()).set(doc.getValue())); |
| 355 | + if (writeBatch == null) { |
| 356 | + writeBatch = collection.getFirestore().batch(); |
| 357 | + } |
| 358 | + |
| 359 | + writeBatch.set(collection.document(doc.getKey()), doc.getValue()); |
| 360 | + writeBatchSize++; |
| 361 | + |
| 362 | + // Write batches are capped at 500 writes. Use 400 just to be safe. |
| 363 | + if (writeBatchSize == 400) { |
| 364 | + waitFor(writeBatch.commit()); |
| 365 | + writeBatch = null; |
| 366 | + writeBatchSize = 0; |
| 367 | + } |
| 368 | + } |
| 369 | + |
| 370 | + if (writeBatch != null) { |
| 371 | + waitFor(writeBatch.commit()); |
352 | 372 | }
|
353 | 373 | }
|
354 | 374 |
|
|
0 commit comments