FIRTransaction.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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+Internal.h"
  17. #import "FIRDocumentReference+Internal.h"
  18. #import "FIRDocumentSnapshot+Internal.h"
  19. #import "FIRFirestore+Internal.h"
  20. #import "FIRSetOptions+Internal.h"
  21. #import "FSTAssert.h"
  22. #import "FSTDocument.h"
  23. #import "FSTTransaction.h"
  24. #import "FSTUsageValidation.h"
  25. #import "FSTUserDataConverter.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 = [self.firestore.dataConverter parsedSetData:data options:options];
  59. [self.internalTransaction setData:parsed forDocument:document.key];
  60. return self;
  61. }
  62. - (FIRTransaction *)updateData:(NSDictionary<id, id> *)fields
  63. forDocument:(FIRDocumentReference *)document {
  64. [self validateReference:document];
  65. FSTParsedUpdateData *parsed = [self.firestore.dataConverter parsedUpdateData:fields];
  66. [self.internalTransaction updateData:parsed forDocument:document.key];
  67. return self;
  68. }
  69. - (FIRTransaction *)deleteDocument:(FIRDocumentReference *)document {
  70. [self validateReference:document];
  71. [self.internalTransaction deleteDocument:document.key];
  72. return self;
  73. }
  74. - (void)getDocument:(FIRDocumentReference *)document
  75. completion:(void (^)(FIRDocumentSnapshot *_Nullable document,
  76. NSError *_Nullable error))completion {
  77. [self validateReference:document];
  78. [self.internalTransaction
  79. lookupDocumentsForKeys:@[ document.key ]
  80. completion:^(NSArray<FSTMaybeDocument *> *_Nullable documents,
  81. NSError *_Nullable error) {
  82. if (error) {
  83. completion(nil, error);
  84. return;
  85. }
  86. FSTAssert(documents.count == 1,
  87. @"Mismatch in docs returned from document lookup.");
  88. FSTMaybeDocument *internalDoc = documents.firstObject;
  89. if ([internalDoc isKindOfClass:[FSTDeletedDocument class]]) {
  90. completion(nil, nil);
  91. return;
  92. }
  93. FIRDocumentSnapshot *doc =
  94. [FIRDocumentSnapshot snapshotWithFirestore:self.firestore
  95. documentKey:internalDoc.key
  96. document:(FSTDocument *)internalDoc
  97. fromCache:NO];
  98. completion(doc, nil);
  99. }];
  100. }
  101. - (FIRDocumentSnapshot *_Nullable)getDocument:(FIRDocumentReference *)document
  102. error:(NSError *__autoreleasing *)error {
  103. [self validateReference:document];
  104. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  105. __block FIRDocumentSnapshot *result;
  106. // We have to explicitly assign the innerError into a local to cause it to retain correctly.
  107. __block NSError *outerError = nil;
  108. [self getDocument:document
  109. completion:^(FIRDocumentSnapshot *_Nullable snapshot, NSError *_Nullable innerError) {
  110. result = snapshot;
  111. outerError = innerError;
  112. dispatch_semaphore_signal(semaphore);
  113. }];
  114. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  115. if (error) {
  116. *error = outerError;
  117. }
  118. return result;
  119. }
  120. - (void)validateReference:(FIRDocumentReference *)reference {
  121. if (reference.firestore != self.firestore) {
  122. FSTThrowInvalidArgument(@"Provided document reference is from a different Firestore instance.");
  123. }
  124. }
  125. @end
  126. NS_ASSUME_NONNULL_END