FIRTransaction.m 5.7 KB

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