FIRWriteBatch.m 3.8 KB

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