FIRWriteBatch.mm 4.6 KB

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