FIRTransaction.mm 7.1 KB

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