FIRTransaction.mm 7.0 KB

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