FIRTransaction.mm 7.0 KB

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