Skip to content

Commit b8a9c80

Browse files
committed
FSTIntegrationTestCase.mm: use a WriteBatch to create documents, for efficiency
1 parent e4f358c commit b8a9c80

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import <FirebaseFirestore/FIRQuerySnapshot.h>
2626
#import <FirebaseFirestore/FIRSnapshotMetadata.h>
2727
#import <FirebaseFirestore/FIRTransaction.h>
28+
#import <FirebaseFirestore/FIRWriteBatch.h>
2829

2930
#include <memory>
3031
#include <string>
@@ -366,11 +367,48 @@ - (FIRCollectionReference *)collectionRefWithDocuments:
366367

367368
- (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents
368369
toCollection:(FIRCollectionReference *)collection {
370+
NSMutableArray<XCTestExpectation *> *commits = [[NSMutableArray alloc] init];
371+
__block FIRWriteBatch *writeBatch = nil;
372+
__block int writeBatchSize = 0;
373+
369374
[documents enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary<NSString *, id> *value,
370375
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+
}
373396
}];
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+
}
374412
}
375413

376414
- (void)readerAndWriterOnDocumentRef:(void (^)(FIRDocumentReference *readerRef,

0 commit comments

Comments
 (0)