FIRTransaction.mm 7.0 KB

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