|
25 | 25 | #import <FirebaseFirestore/FIRQuerySnapshot.h> |
26 | 26 | #import <FirebaseFirestore/FIRSnapshotMetadata.h> |
27 | 27 | #import <FirebaseFirestore/FIRTransaction.h> |
| 28 | +#import <FirebaseFirestore/FIRWriteBatch.h> |
28 | 29 |
|
29 | 30 | #include <memory> |
30 | 31 | #include <string> |
@@ -366,11 +367,48 @@ - (FIRCollectionReference *)collectionRefWithDocuments: |
366 | 367 |
|
367 | 368 | - (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents |
368 | 369 | toCollection:(FIRCollectionReference *)collection { |
| 370 | + NSMutableArray<XCTestExpectation *> *commits = [[NSMutableArray alloc] init]; |
| 371 | + __block FIRWriteBatch *writeBatch = nil; |
| 372 | + __block int writeBatchSize = 0; |
| 373 | + |
369 | 374 | [documents enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary<NSString *, id> *value, |
370 | 375 | BOOL *) { |
371 | | - FIRDocumentReference *ref = [collection documentWithPath:key]; |
372 | | - [self writeDocumentRef:ref data:value]; |
| 376 | + if (writeBatch == nil) { |
| 377 | + writeBatch = [collection.firestore batch]; |
| 378 | + } |
| 379 | + |
| 380 | + [writeBatch setData:value forDocument:[collection documentWithPath:key]]; |
| 381 | + writeBatchSize++; |
| 382 | + |
| 383 | + // Write batches are capped at 500 writes. Use 400 just to be safe. |
| 384 | + if (writeBatchSize == 400) { |
| 385 | + XCTestExpectation *commitExpectation = [self expectationWithDescription:@"WriteBatch commit"]; |
| 386 | + [writeBatch commitWithCompletion:^(NSError *_Nullable error) { |
| 387 | + [commitExpectation fulfill]; |
| 388 | + if (error != nil) { |
| 389 | + XCTFail(@"WriteBatch commit failed: %@", error); |
| 390 | + } |
| 391 | + }]; |
| 392 | + [commits addObject:commitExpectation]; |
| 393 | + writeBatch = nil; |
| 394 | + writeBatchSize = 0; |
| 395 | + } |
373 | 396 | }]; |
| 397 | + |
| 398 | + if (writeBatch != nil) { |
| 399 | + XCTestExpectation *commitExpectation = [self expectationWithDescription:@"WriteBatch commit"]; |
| 400 | + [writeBatch commitWithCompletion:^(NSError *_Nullable error) { |
| 401 | + [commitExpectation fulfill]; |
| 402 | + if (error != nil) { |
| 403 | + XCTFail(@"WriteBatch commit failed: %@", error); |
| 404 | + } |
| 405 | + }]; |
| 406 | + [commits addObject:commitExpectation]; |
| 407 | + } |
| 408 | + |
| 409 | + for (XCTestExpectation *commitExpectation in commits) { |
| 410 | + [self awaitExpectation:commitExpectation]; |
| 411 | + } |
374 | 412 | } |
375 | 413 |
|
376 | 414 | - (void)readerAndWriterOnDocumentRef:(void (^)(FIRDocumentReference *readerRef, |
|
0 commit comments