FIRTransaction.mm 7.1 KB

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