FIRWriteBatch.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 ? [self.firestore.dataConverter parsedMergeData:data]
  57. : [self.firestore.dataConverter parsedSetData:data];
  58. [self.mutations
  59. addObjectsFromArray:[parsed mutationsWithKey:document.key precondition:Precondition::None()]];
  60. return self;
  61. }
  62. - (FIRWriteBatch *)updateData:(NSDictionary<id, id> *)fields
  63. forDocument:(FIRDocumentReference *)document {
  64. [self verifyNotCommitted];
  65. [self validateReference:document];
  66. FSTParsedUpdateData *parsed = [self.firestore.dataConverter parsedUpdateData:fields];
  67. [self.mutations addObjectsFromArray:[parsed mutationsWithKey:document.key
  68. precondition:Precondition::Exists(true)]];
  69. return self;
  70. }
  71. - (FIRWriteBatch *)deleteDocument:(FIRDocumentReference *)document {
  72. [self verifyNotCommitted];
  73. [self validateReference:document];
  74. [self.mutations addObject:[[FSTDeleteMutation alloc] initWithKey:document.key
  75. precondition:Precondition::None()]];
  76. return self;
  77. }
  78. - (void)commit {
  79. [self commitWithCompletion:nil];
  80. }
  81. - (void)commitWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
  82. [self verifyNotCommitted];
  83. self.committed = TRUE;
  84. [self.firestore.client writeMutations:self.mutations completion:completion];
  85. }
  86. - (void)verifyNotCommitted {
  87. if (self.committed) {
  88. FSTThrowInvalidUsage(@"FIRIllegalStateException",
  89. @"A write batch can no longer be used after commit has been called.");
  90. }
  91. }
  92. - (void)validateReference:(FIRDocumentReference *)reference {
  93. if (reference.firestore != self.firestore) {
  94. FSTThrowInvalidArgument(@"Provided document reference is from a different Firestore instance.");
  95. }
  96. }
  97. @end
  98. NS_ASSUME_NONNULL_END