FIRTransaction.mm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "Firestore/Source/API/FIRTransaction+Internal.h"
  17. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  18. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  19. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  20. #import "Firestore/Source/API/FSTUserDataConverter.h"
  21. #import "Firestore/Source/Core/FSTTransaction.h"
  22. #import "Firestore/Source/Model/FSTDocument.h"
  23. #import "Firestore/Source/Util/FSTUsageValidation.h"
  24. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  25. NS_ASSUME_NONNULL_BEGIN
  26. #pragma mark - FIRTransaction
  27. @interface FIRTransaction ()
  28. - (instancetype)initWithTransaction:(FSTTransaction *)transaction
  29. firestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
  30. @property(nonatomic, strong, readonly) FSTTransaction *internalTransaction;
  31. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  32. @end
  33. @implementation FIRTransaction (Internal)
  34. + (instancetype)transactionWithFSTTransaction:(FSTTransaction *)transaction
  35. firestore:(FIRFirestore *)firestore {
  36. return [[FIRTransaction alloc] initWithTransaction:transaction firestore:firestore];
  37. }
  38. @end
  39. @implementation FIRTransaction
  40. - (instancetype)initWithTransaction:(FSTTransaction *)transaction
  41. firestore:(FIRFirestore *)firestore {
  42. self = [super init];
  43. if (self) {
  44. _internalTransaction = transaction;
  45. _firestore = firestore;
  46. }
  47. return self;
  48. }
  49. - (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
  50. forDocument:(FIRDocumentReference *)document {
  51. return [self setData:data forDocument:document merge:NO];
  52. }
  53. - (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
  54. forDocument:(FIRDocumentReference *)document
  55. merge:(BOOL)merge {
  56. [self validateReference:document];
  57. FSTParsedSetData *parsed = merge
  58. ? [self.firestore.dataConverter parsedMergeData:data fieldMask:nil]
  59. : [self.firestore.dataConverter parsedSetData:data];
  60. [self.internalTransaction setData:parsed forDocument:document.key];
  61. return self;
  62. }
  63. - (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
  64. forDocument:(FIRDocumentReference *)document
  65. mergeFields:(NSArray<id> *)mergeFields {
  66. [self validateReference:document];
  67. FSTParsedSetData *parsed =
  68. [self.firestore.dataConverter parsedMergeData:data fieldMask:mergeFields];
  69. [self.internalTransaction setData:parsed forDocument:document.key];
  70. return self;
  71. }
  72. - (FIRTransaction *)updateData:(NSDictionary<id, id> *)fields
  73. forDocument:(FIRDocumentReference *)document {
  74. [self validateReference:document];
  75. FSTParsedUpdateData *parsed = [self.firestore.dataConverter parsedUpdateData:fields];
  76. [self.internalTransaction updateData:parsed forDocument:document.key];
  77. return self;
  78. }
  79. - (FIRTransaction *)deleteDocument:(FIRDocumentReference *)document {
  80. [self validateReference:document];
  81. [self.internalTransaction deleteDocument:document.key];
  82. return self;
  83. }
  84. - (void)getDocument:(FIRDocumentReference *)document
  85. completion:(void (^)(FIRDocumentSnapshot *_Nullable document,
  86. NSError *_Nullable error))completion {
  87. [self validateReference:document];
  88. [self.internalTransaction
  89. lookupDocumentsForKeys:{document.key}
  90. completion:^(NSArray<FSTMaybeDocument *> *_Nullable documents,
  91. NSError *_Nullable error) {
  92. if (error) {
  93. completion(nil, error);
  94. return;
  95. }
  96. HARD_ASSERT(documents.count == 1,
  97. "Mismatch in docs returned from document lookup.");
  98. FSTMaybeDocument *internalDoc = documents.firstObject;
  99. if ([internalDoc isKindOfClass:[FSTDeletedDocument class]]) {
  100. completion(nil, nil);
  101. return;
  102. }
  103. FIRDocumentSnapshot *doc =
  104. [FIRDocumentSnapshot snapshotWithFirestore:self.firestore
  105. documentKey:internalDoc.key
  106. document:(FSTDocument *)internalDoc
  107. fromCache:NO];
  108. completion(doc, nil);
  109. }];
  110. }
  111. - (FIRDocumentSnapshot *_Nullable)getDocument:(FIRDocumentReference *)document
  112. error:(NSError *__autoreleasing *)error {
  113. [self validateReference:document];
  114. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  115. __block FIRDocumentSnapshot *result;
  116. // We have to explicitly assign the innerError into a local to cause it to retain correctly.
  117. __block NSError *outerError = nil;
  118. [self getDocument:document
  119. completion:^(FIRDocumentSnapshot *_Nullable snapshot, NSError *_Nullable innerError) {
  120. result = snapshot;
  121. outerError = innerError;
  122. dispatch_semaphore_signal(semaphore);
  123. }];
  124. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  125. if (error) {
  126. *error = outerError;
  127. }
  128. return result;
  129. }
  130. - (void)validateReference:(FIRDocumentReference *)reference {
  131. if (reference.firestore != self.firestore) {
  132. FSTThrowInvalidArgument(@"Provided document reference is from a different Firestore instance.");
  133. }
  134. }
  135. @end
  136. NS_ASSUME_NONNULL_END