FIRTransaction.mm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "FIRTransaction.h"
  17. #include <utility>
  18. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  19. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  20. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  21. #import "Firestore/Source/API/FIRTransaction+Internal.h"
  22. #import "Firestore/Source/API/FSTUserDataConverter.h"
  23. #import "Firestore/Source/Core/FSTTransaction.h"
  24. #import "Firestore/Source/Model/FSTDocument.h"
  25. #import "Firestore/Source/Util/FSTUsageValidation.h"
  26. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  27. using firebase::firestore::core::ParsedSetData;
  28. using firebase::firestore::core::ParsedUpdateData;
  29. NS_ASSUME_NONNULL_BEGIN
  30. #pragma mark - FIRTransaction
  31. @interface FIRTransaction ()
  32. - (instancetype)initWithTransaction:(FSTTransaction *)transaction
  33. firestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
  34. @property(nonatomic, strong, readonly) FSTTransaction *internalTransaction;
  35. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  36. @end
  37. @implementation FIRTransaction (Internal)
  38. + (instancetype)transactionWithFSTTransaction:(FSTTransaction *)transaction
  39. firestore:(FIRFirestore *)firestore {
  40. return [[FIRTransaction alloc] initWithTransaction:transaction firestore:firestore];
  41. }
  42. @end
  43. @implementation FIRTransaction
  44. - (instancetype)initWithTransaction:(FSTTransaction *)transaction
  45. firestore:(FIRFirestore *)firestore {
  46. self = [super init];
  47. if (self) {
  48. _internalTransaction = transaction;
  49. _firestore = firestore;
  50. }
  51. return self;
  52. }
  53. - (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
  54. forDocument:(FIRDocumentReference *)document {
  55. return [self setData:data forDocument:document merge:NO];
  56. }
  57. - (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
  58. forDocument:(FIRDocumentReference *)document
  59. merge:(BOOL)merge {
  60. [self validateReference:document];
  61. ParsedSetData parsed = merge ? [self.firestore.dataConverter parsedMergeData:data fieldMask:nil]
  62. : [self.firestore.dataConverter parsedSetData:data];
  63. [self.internalTransaction setData:std::move(parsed) forDocument:document.key];
  64. return self;
  65. }
  66. - (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
  67. forDocument:(FIRDocumentReference *)document
  68. mergeFields:(NSArray<id> *)mergeFields {
  69. [self validateReference:document];
  70. ParsedSetData parsed = [self.firestore.dataConverter parsedMergeData:data fieldMask:mergeFields];
  71. [self.internalTransaction setData:std::move(parsed) forDocument:document.key];
  72. return self;
  73. }
  74. - (FIRTransaction *)updateData:(NSDictionary<id, id> *)fields
  75. forDocument:(FIRDocumentReference *)document {
  76. [self validateReference:document];
  77. ParsedUpdateData parsed = [self.firestore.dataConverter parsedUpdateData:fields];
  78. [self.internalTransaction updateData:std::move(parsed) forDocument:document.key];
  79. return self;
  80. }
  81. - (FIRTransaction *)deleteDocument:(FIRDocumentReference *)document {
  82. [self validateReference:document];
  83. [self.internalTransaction deleteDocument:document.key];
  84. return self;
  85. }
  86. - (void)getDocument:(FIRDocumentReference *)document
  87. completion:(void (^)(FIRDocumentSnapshot *_Nullable document,
  88. NSError *_Nullable error))completion {
  89. [self validateReference:document];
  90. [self.internalTransaction
  91. lookupDocumentsForKeys:{document.key}
  92. completion:^(NSArray<FSTMaybeDocument *> *_Nullable documents,
  93. NSError *_Nullable error) {
  94. if (error) {
  95. completion(nil, error);
  96. return;
  97. }
  98. HARD_ASSERT(documents.count == 1,
  99. "Mismatch in docs returned from document lookup.");
  100. FSTMaybeDocument *internalDoc = documents.firstObject;
  101. if ([internalDoc isKindOfClass:[FSTDeletedDocument class]]) {
  102. FIRDocumentSnapshot *doc =
  103. [FIRDocumentSnapshot snapshotWithFirestore:self.firestore
  104. documentKey:document.key
  105. document:nil
  106. fromCache:NO
  107. hasPendingWrites:NO];
  108. completion(doc, nil);
  109. } else if ([internalDoc isKindOfClass:[FSTDocument class]]) {
  110. FIRDocumentSnapshot *doc =
  111. [FIRDocumentSnapshot snapshotWithFirestore:self.firestore
  112. documentKey:internalDoc.key
  113. document:(FSTDocument *)internalDoc
  114. fromCache:NO
  115. hasPendingWrites:NO];
  116. completion(doc, nil);
  117. } else {
  118. HARD_FAIL("BatchGetDocumentsRequest returned unexpected document type: %s",
  119. NSStringFromClass([internalDoc class]));
  120. }
  121. }];
  122. }
  123. - (FIRDocumentSnapshot *_Nullable)getDocument:(FIRDocumentReference *)document
  124. error:(NSError *__autoreleasing *)error {
  125. [self validateReference:document];
  126. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  127. __block FIRDocumentSnapshot *result;
  128. // We have to explicitly assign the innerError into a local to cause it to retain correctly.
  129. __block NSError *outerError = nil;
  130. [self getDocument:document
  131. completion:^(FIRDocumentSnapshot *_Nullable snapshot, NSError *_Nullable innerError) {
  132. result = snapshot;
  133. outerError = innerError;
  134. dispatch_semaphore_signal(semaphore);
  135. }];
  136. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  137. if (error) {
  138. *error = outerError;
  139. }
  140. return result;
  141. }
  142. - (void)validateReference:(FIRDocumentReference *)reference {
  143. if (reference.firestore != self.firestore) {
  144. FSTThrowInvalidArgument(@"Provided document reference is from a different Firestore instance.");
  145. }
  146. }
  147. @end
  148. NS_ASSUME_NONNULL_END