FIRWriteBatch.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <utility>
  18. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  19. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  20. #import "Firestore/Source/API/FIRWriteBatch+Internal.h"
  21. #import "Firestore/Source/API/FSTUserDataConverter.h"
  22. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  23. #import "Firestore/Source/Model/FSTMutation.h"
  24. #import "Firestore/Source/Util/FSTUsageValidation.h"
  25. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  26. using firebase::firestore::core::ParsedSetData;
  27. using firebase::firestore::core::ParsedUpdateData;
  28. using firebase::firestore::model::Precondition;
  29. NS_ASSUME_NONNULL_BEGIN
  30. #pragma mark - FIRWriteBatch
  31. @interface FIRWriteBatch ()
  32. - (instancetype)initWithFirestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
  33. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  34. @property(nonatomic, strong, readonly) NSMutableArray<FSTMutation *> *mutations;
  35. @property(nonatomic, assign) BOOL committed;
  36. @end
  37. @implementation FIRWriteBatch (Internal)
  38. + (instancetype)writeBatchWithFirestore:(FIRFirestore *)firestore {
  39. return [[FIRWriteBatch alloc] initWithFirestore:firestore];
  40. }
  41. @end
  42. @implementation FIRWriteBatch
  43. - (instancetype)initWithFirestore:(FIRFirestore *)firestore {
  44. self = [super init];
  45. if (self) {
  46. _firestore = firestore;
  47. _mutations = [NSMutableArray array];
  48. }
  49. return self;
  50. }
  51. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  52. forDocument:(FIRDocumentReference *)document {
  53. return [self setData:data forDocument:document merge:NO];
  54. }
  55. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  56. forDocument:(FIRDocumentReference *)document
  57. merge:(BOOL)merge {
  58. [self verifyNotCommitted];
  59. [self validateReference:document];
  60. ParsedSetData parsed = merge ? [self.firestore.dataConverter parsedMergeData:data fieldMask:nil]
  61. : [self.firestore.dataConverter parsedSetData:data];
  62. [self.mutations
  63. addObjectsFromArray:std::move(parsed).ToMutations(document.key, Precondition::None())];
  64. return self;
  65. }
  66. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  67. forDocument:(FIRDocumentReference *)document
  68. mergeFields:(NSArray<id> *)mergeFields {
  69. [self verifyNotCommitted];
  70. [self validateReference:document];
  71. ParsedSetData parsed = [self.firestore.dataConverter parsedMergeData:data fieldMask:mergeFields];
  72. [self.mutations
  73. addObjectsFromArray:std::move(parsed).ToMutations(document.key, Precondition::None())];
  74. return self;
  75. }
  76. - (FIRWriteBatch *)updateData:(NSDictionary<id, id> *)fields
  77. forDocument:(FIRDocumentReference *)document {
  78. [self verifyNotCommitted];
  79. [self validateReference:document];
  80. ParsedUpdateData parsed = [self.firestore.dataConverter parsedUpdateData:fields];
  81. [self.mutations
  82. addObjectsFromArray:std::move(parsed).ToMutations(document.key, Precondition::Exists(true))];
  83. return self;
  84. }
  85. - (FIRWriteBatch *)deleteDocument:(FIRDocumentReference *)document {
  86. [self verifyNotCommitted];
  87. [self validateReference:document];
  88. [self.mutations addObject:[[FSTDeleteMutation alloc] initWithKey:document.key
  89. precondition:Precondition::None()]];
  90. return self;
  91. }
  92. - (void)commit {
  93. [self commitWithCompletion:nil];
  94. }
  95. - (void)commitWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
  96. [self verifyNotCommitted];
  97. self.committed = TRUE;
  98. [self.firestore.client writeMutations:self.mutations completion:completion];
  99. }
  100. - (void)verifyNotCommitted {
  101. if (self.committed) {
  102. FSTThrowInvalidUsage(@"FIRIllegalStateException",
  103. @"A write batch can no longer be used after commit has been called.");
  104. }
  105. }
  106. - (void)validateReference:(FIRDocumentReference *)reference {
  107. if (reference.firestore != self.firestore) {
  108. FSTThrowInvalidArgument(@"Provided document reference is from a different Firestore instance.");
  109. }
  110. }
  111. @end
  112. NS_ASSUME_NONNULL_END