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/FSTUserDataReader.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::util::MakeNSError;
  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.dataReader parsedMergeData:data fieldMask:nil]
  74. : [self.firestore.dataReader 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.dataReader 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.dataReader 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<Document>> &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 Document &internalDoc = documents.front();
  112. if (internalDoc->is_found_document()) {
  113. FIRDocumentSnapshot *doc =
  114. [[FIRDocumentSnapshot alloc] initWithFirestore:self.firestore
  115. documentKey:internalDoc->key()
  116. document:internalDoc
  117. fromCache:false
  118. hasPendingWrites:false];
  119. completion(doc, nil);
  120. } else if (internalDoc->is_no_document()) {
  121. FIRDocumentSnapshot *doc = [[FIRDocumentSnapshot alloc] initWithFirestore:self.firestore
  122. documentKey:document.key
  123. document:absl::nullopt
  124. fromCache:false
  125. hasPendingWrites:false];
  126. completion(doc, nil);
  127. } else {
  128. HARD_FAIL("BatchGetDocumentsRequest returned unexpected document type: %s",
  129. internalDoc.ToString());
  130. }
  131. });
  132. }
  133. - (FIRDocumentSnapshot *_Nullable)getDocument:(FIRDocumentReference *)document
  134. error:(NSError *__autoreleasing *)error {
  135. [self validateReference:document];
  136. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  137. __block FIRDocumentSnapshot *result;
  138. // We have to explicitly assign the innerError into a local to cause it to retain correctly.
  139. __block NSError *outerError = nil;
  140. [self getDocument:document
  141. completion:^(FIRDocumentSnapshot *_Nullable snapshot, NSError *_Nullable innerError) {
  142. result = snapshot;
  143. outerError = innerError;
  144. dispatch_semaphore_signal(semaphore);
  145. }];
  146. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  147. if (error) {
  148. *error = outerError;
  149. }
  150. return result;
  151. }
  152. - (void)validateReference:(FIRDocumentReference *)reference {
  153. if (reference.firestore != self.firestore) {
  154. ThrowInvalidArgument("Provided document reference is from a different Cloud Firestore "
  155. "instance.");
  156. }
  157. }
  158. @end
  159. NS_ASSUME_NONNULL_END