aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar zxu <zxu@google.com>2017-12-11 15:37:01 -0500
committerGravatar GitHub <noreply@github.com>2017-12-11 15:37:01 -0500
commit54ba423c87d6e0a409e4fa3dc95cd81ca16b97f7 (patch)
treec232df19ef070105f7db11c35e7ecdf1d9b2ba07
parentc6b8c5288c2d154df0de98477ed15c54e2084fbb (diff)
Revisit commit method in FIRWriteBatch (#541)
* revisit FIRWriteBatch commit * make commitWithCompletion completion nullable; * add commit; * add unit test; * add swift build test for commit; * update CHANGELOG.
-rw-r--r--Firestore/CHANGELOG.md1
-rw-r--r--Firestore/Example/SwiftBuildTest/main.swift26
-rw-r--r--Firestore/Example/Tests/Integration/API/FIRWriteBatchTests.m17
-rw-r--r--Firestore/Source/API/FIRWriteBatch.m6
-rw-r--r--Firestore/Source/Public/FIRWriteBatch.h7
5 files changed, 55 insertions, 2 deletions
diff --git a/Firestore/CHANGELOG.md b/Firestore/CHANGELOG.md
index 32cbbfc..1a8eb07 100644
--- a/Firestore/CHANGELOG.md
+++ b/Firestore/CHANGELOG.md
@@ -2,6 +2,7 @@
- [changed] Firestore no longer has a direct dependency on FirebaseAuth.
- [changed] Removed the includeMetadataChanges property in FIRDocumentListenOptions
to avoid confusion with the factory method of the same name.
+- [changed] Added a commit method that takes no completion handler to FIRWriteBatch.
- [fixed] Fixed a crash when using path names with international characters
with persistence enabled.
diff --git a/Firestore/Example/SwiftBuildTest/main.swift b/Firestore/Example/SwiftBuildTest/main.swift
index bea8b56..475c1b2 100644
--- a/Firestore/Example/SwiftBuildTest/main.swift
+++ b/Firestore/Example/SwiftBuildTest/main.swift
@@ -27,6 +27,8 @@ func main() {
writeDocument(at: documentRef);
+ writeDocuments(at: documentRef, database: db);
+
addDocument(to: collectionRef);
readDocument(at: documentRef);
@@ -129,6 +131,30 @@ func writeDocument(at docRef: DocumentReference) {
}
}
+func writeDocuments(at docRef: DocumentReference, database db: Firestore) {
+ var batch: WriteBatch;
+
+ batch = db.batch();
+ batch.setData(["a" : "b"], forDocument:docRef);
+ batch.setData(["c" : "d"], forDocument:docRef);
+ // commit without completion callback.
+ batch.commit();
+ print("Batch write without completion complete!");
+
+ batch = db.batch();
+ batch.setData(["a" : "b"], forDocument:docRef);
+ batch.setData(["c" : "d"], forDocument:docRef);
+ // commit with completion callback via trailing closure syntax.
+ batch.commit() { error in
+ if let error = error {
+ print("Uh oh! \(error)");
+ return;
+ }
+ print("Batch write callback complete!");
+ }
+ print("Batch write with completion complete!");
+}
+
func addDocument(to collectionRef: CollectionReference) {
collectionRef.addDocument(data: ["foo": 42]);
diff --git a/Firestore/Example/Tests/Integration/API/FIRWriteBatchTests.m b/Firestore/Example/Tests/Integration/API/FIRWriteBatchTests.m
index 562c29f..23ae7bc 100644
--- a/Firestore/Example/Tests/Integration/API/FIRWriteBatchTests.m
+++ b/Firestore/Example/Tests/Integration/API/FIRWriteBatchTests.m
@@ -35,6 +35,23 @@
[self awaitExpectations];
}
+- (void)testCommitWithoutCompletionHandler {
+ FIRDocumentReference *doc = [self documentRef];
+ FIRWriteBatch *batch1 = [doc.firestore batch];
+ [batch1 setData:@{@"aa" : @"bb"} forDocument:doc];
+ [batch1 commitWithCompletion:nil];
+ FIRDocumentSnapshot *snapshot1 = [self readDocumentForRef:doc];
+ XCTAssertTrue(snapshot1.exists);
+ XCTAssertEqualObjects(snapshot1.data, @{@"aa" : @"bb"});
+
+ FIRWriteBatch *batch2 = [doc.firestore batch];
+ [batch2 setData:@{@"cc" : @"dd"} forDocument:doc];
+ [batch2 commit];
+ FIRDocumentSnapshot *snapshot2 = [self readDocumentForRef:doc];
+ XCTAssertTrue(snapshot2.exists);
+ XCTAssertEqualObjects(snapshot2.data, @{@"cc" : @"dd"});
+}
+
- (void)testSetDocuments {
FIRDocumentReference *doc = [self documentRef];
XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
diff --git a/Firestore/Source/API/FIRWriteBatch.m b/Firestore/Source/API/FIRWriteBatch.m
index b918a9a..b1cfa09 100644
--- a/Firestore/Source/API/FIRWriteBatch.m
+++ b/Firestore/Source/API/FIRWriteBatch.m
@@ -93,7 +93,11 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
-- (void)commitWithCompletion:(void (^)(NSError *_Nullable error))completion {
+- (void)commit {
+ [self commitWithCompletion:nil];
+}
+
+- (void)commitWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
[self verifyNotCommitted];
self.committed = TRUE;
[self.firestore.client writeMutations:self.mutations completion:completion];
diff --git a/Firestore/Source/Public/FIRWriteBatch.h b/Firestore/Source/Public/FIRWriteBatch.h
index 5f0034c..8ff1bec 100644
--- a/Firestore/Source/Public/FIRWriteBatch.h
+++ b/Firestore/Source/Public/FIRWriteBatch.h
@@ -94,6 +94,11 @@ NS_SWIFT_NAME(WriteBatch)
/**
* Commits all of the writes in this write batch as a single atomic unit.
+ */
+- (void)commit;
+
+/**
+ * Commits all of the writes in this write batch as a single atomic unit.
*
* @param completion A block to be called once all of the writes in the batch have been
* successfully written to the backend as an atomic unit. This block will only execute
@@ -101,7 +106,7 @@ NS_SWIFT_NAME(WriteBatch)
* completion handler will not be called when the device is offline, though local
* changes will be visible immediately.
*/
-- (void)commitWithCompletion:(void (^)(NSError *_Nullable error))completion;
+- (void)commitWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
@end