FIRWriteBatch.mm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRWriteBatch.h"
  17. #include <algorithm>
  18. #include <utility>
  19. #include <vector>
  20. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  21. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  22. #import "Firestore/Source/API/FIRWriteBatch+Internal.h"
  23. #import "Firestore/Source/API/FSTUserDataConverter.h"
  24. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  25. #import "Firestore/Source/Model/FSTMutation.h"
  26. #import "Firestore/Source/Util/FSTUsageValidation.h"
  27. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  28. using firebase::firestore::core::ParsedSetData;
  29. using firebase::firestore::core::ParsedUpdateData;
  30. using firebase::firestore::model::Precondition;
  31. NS_ASSUME_NONNULL_BEGIN
  32. #pragma mark - FIRWriteBatch
  33. @interface FIRWriteBatch ()
  34. - (instancetype)initWithFirestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
  35. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  36. @property(nonatomic, assign) BOOL committed;
  37. @end
  38. @implementation FIRWriteBatch (Internal)
  39. + (instancetype)writeBatchWithFirestore:(FIRFirestore *)firestore {
  40. return [[FIRWriteBatch alloc] initWithFirestore:firestore];
  41. }
  42. @end
  43. @implementation FIRWriteBatch {
  44. std::vector<FSTMutation *> _mutations;
  45. }
  46. - (instancetype)initWithFirestore:(FIRFirestore *)firestore {
  47. self = [super init];
  48. if (self) {
  49. _firestore = firestore;
  50. }
  51. return self;
  52. }
  53. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  54. forDocument:(FIRDocumentReference *)document {
  55. return [self setData:data forDocument:document merge:NO];
  56. }
  57. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  58. forDocument:(FIRDocumentReference *)document
  59. merge:(BOOL)merge {
  60. [self verifyNotCommitted];
  61. [self validateReference:document];
  62. ParsedSetData parsed = merge ? [self.firestore.dataConverter parsedMergeData:data fieldMask:nil]
  63. : [self.firestore.dataConverter parsedSetData:data];
  64. std::vector<FSTMutation *> append_mutations =
  65. std::move(parsed).ToMutations(document.key, Precondition::None());
  66. std::move(append_mutations.begin(), append_mutations.end(), std::back_inserter(_mutations));
  67. return self;
  68. }
  69. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  70. forDocument:(FIRDocumentReference *)document
  71. mergeFields:(NSArray<id> *)mergeFields {
  72. [self verifyNotCommitted];
  73. [self validateReference:document];
  74. ParsedSetData parsed = [self.firestore.dataConverter parsedMergeData:data fieldMask:mergeFields];
  75. std::vector<FSTMutation *> append_mutations =
  76. std::move(parsed).ToMutations(document.key, Precondition::None());
  77. std::move(append_mutations.begin(), append_mutations.end(), std::back_inserter(_mutations));
  78. return self;
  79. }
  80. - (FIRWriteBatch *)updateData:(NSDictionary<id, id> *)fields
  81. forDocument:(FIRDocumentReference *)document {
  82. [self verifyNotCommitted];
  83. [self validateReference:document];
  84. ParsedUpdateData parsed = [self.firestore.dataConverter parsedUpdateData:fields];
  85. std::vector<FSTMutation *> append_mutations =
  86. std::move(parsed).ToMutations(document.key, Precondition::Exists(true));
  87. std::move(append_mutations.begin(), append_mutations.end(), std::back_inserter(_mutations));
  88. return self;
  89. }
  90. - (FIRWriteBatch *)deleteDocument:(FIRDocumentReference *)document {
  91. [self verifyNotCommitted];
  92. [self validateReference:document];
  93. _mutations.push_back([[FSTDeleteMutation alloc] initWithKey:document.key
  94. precondition:Precondition::None()]);
  95. ;
  96. return self;
  97. }
  98. - (void)commit {
  99. [self commitWithCompletion:nil];
  100. }
  101. - (void)commitWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
  102. [self verifyNotCommitted];
  103. self.committed = TRUE;
  104. [self.firestore.client writeMutations:std::move(_mutations) completion:completion];
  105. }
  106. - (void)verifyNotCommitted {
  107. if (self.committed) {
  108. FSTThrowInvalidUsage(@"FIRIllegalStateException",
  109. @"A write batch can no longer be used after commit has been called.");
  110. }
  111. }
  112. - (void)validateReference:(FIRDocumentReference *)reference {
  113. if (reference.firestore != self.firestore) {
  114. FSTThrowInvalidArgument(@"Provided document reference is from a different Firestore instance.");
  115. }
  116. }
  117. @end
  118. NS_ASSUME_NONNULL_END