FIRTransaction.mm 6.9 KB

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