FIRTransaction.mm 7.1 KB

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