FIRDocumentReference.mm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "FIRDocumentReference.h"
  17. #include <memory>
  18. #include <utility>
  19. #import "FIRFirestoreErrors.h"
  20. #import "FIRFirestoreSource.h"
  21. #import "Firestore/Source/API/FIRCollectionReference+Internal.h"
  22. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  23. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  24. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  25. #import "Firestore/Source/API/FIRListenerRegistration+Internal.h"
  26. #import "Firestore/Source/API/FSTUserDataConverter.h"
  27. #import "Firestore/Source/Core/FSTEventManager.h"
  28. #import "Firestore/Source/Core/FSTQuery.h"
  29. #import "Firestore/Source/Model/FSTFieldValue.h"
  30. #import "Firestore/Source/Util/FSTUsageValidation.h"
  31. #include "Firestore/core/src/firebase/firestore/api/document_reference.h"
  32. #include "Firestore/core/src/firebase/firestore/api/document_snapshot.h"
  33. #include "Firestore/core/src/firebase/firestore/core/event_listener.h"
  34. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  35. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  36. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  37. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  38. #include "Firestore/core/src/firebase/firestore/util/error_apple.h"
  39. #include "Firestore/core/src/firebase/firestore/util/status.h"
  40. #include "Firestore/core/src/firebase/firestore/util/statusor.h"
  41. #include "Firestore/core/src/firebase/firestore/util/statusor_callback.h"
  42. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  43. namespace util = firebase::firestore::util;
  44. using firebase::firestore::api::DocumentReference;
  45. using firebase::firestore::api::DocumentSnapshot;
  46. using firebase::firestore::api::Firestore;
  47. using firebase::firestore::core::EventListener;
  48. using firebase::firestore::core::ListenOptions;
  49. using firebase::firestore::core::ParsedSetData;
  50. using firebase::firestore::core::ParsedUpdateData;
  51. using firebase::firestore::model::DocumentKey;
  52. using firebase::firestore::model::Precondition;
  53. using firebase::firestore::model::ResourcePath;
  54. using firebase::firestore::util::Status;
  55. using firebase::firestore::util::StatusOr;
  56. using firebase::firestore::util::StatusOrCallback;
  57. NS_ASSUME_NONNULL_BEGIN
  58. #pragma mark - FIRDocumentReference
  59. @implementation FIRDocumentReference {
  60. DocumentReference _documentReference;
  61. }
  62. - (instancetype)initWithReference:(DocumentReference &&)reference {
  63. if (self = [super init]) {
  64. _documentReference = std::move(reference);
  65. }
  66. return self;
  67. }
  68. - (instancetype)initWithPath:(ResourcePath)path firestore:(Firestore *)firestore {
  69. if (path.size() % 2 != 0) {
  70. FSTThrowInvalidArgument(@"Invalid document reference. Document references must have an even "
  71. "number of segments, but %s has %zu",
  72. path.CanonicalString().c_str(), path.size());
  73. }
  74. return [self initWithKey:DocumentKey{std::move(path)} firestore:firestore];
  75. }
  76. - (instancetype)initWithKey:(DocumentKey)key firestore:(Firestore *)firestore {
  77. DocumentReference delegate{std::move(key), firestore};
  78. return [self initWithReference:std::move(delegate)];
  79. }
  80. #pragma mark - NSObject Methods
  81. - (BOOL)isEqual:(nullable id)other {
  82. if (other == self) return YES;
  83. if (![[other class] isEqual:[self class]]) return NO;
  84. return _documentReference == static_cast<FIRDocumentReference *>(other)->_documentReference;
  85. }
  86. - (NSUInteger)hash {
  87. return _documentReference.Hash();
  88. }
  89. #pragma mark - Public Methods
  90. @dynamic firestore;
  91. - (FIRFirestore *)firestore {
  92. return [FIRFirestore recoverFromFirestore:_documentReference.firestore()];
  93. }
  94. - (NSString *)documentID {
  95. return util::WrapNSString(_documentReference.document_id());
  96. }
  97. - (FIRCollectionReference *)parent {
  98. return [FIRCollectionReference referenceWithPath:_documentReference.key().path().PopLast()
  99. firestore:self.firestore];
  100. }
  101. - (NSString *)path {
  102. return util::WrapNSString(_documentReference.Path());
  103. }
  104. - (FIRCollectionReference *)collectionWithPath:(NSString *)collectionPath {
  105. if (!collectionPath) {
  106. FSTThrowInvalidArgument(@"Collection path cannot be nil.");
  107. }
  108. ResourcePath subPath = ResourcePath::FromString(util::MakeString(collectionPath));
  109. ResourcePath path = _documentReference.key().path().Append(subPath);
  110. return [FIRCollectionReference referenceWithPath:path firestore:self.firestore];
  111. }
  112. - (void)setData:(NSDictionary<NSString *, id> *)documentData {
  113. [self setData:documentData merge:NO completion:nil];
  114. }
  115. - (void)setData:(NSDictionary<NSString *, id> *)documentData merge:(BOOL)merge {
  116. [self setData:documentData merge:merge completion:nil];
  117. }
  118. - (void)setData:(NSDictionary<NSString *, id> *)documentData
  119. mergeFields:(NSArray<id> *)mergeFields {
  120. [self setData:documentData mergeFields:mergeFields completion:nil];
  121. }
  122. - (void)setData:(NSDictionary<NSString *, id> *)documentData
  123. completion:(nullable void (^)(NSError *_Nullable error))completion {
  124. [self setData:documentData merge:NO completion:completion];
  125. }
  126. - (void)setData:(NSDictionary<NSString *, id> *)documentData
  127. merge:(BOOL)merge
  128. completion:(nullable void (^)(NSError *_Nullable error))completion {
  129. auto dataConverter = self.firestore.dataConverter;
  130. ParsedSetData parsed = merge ? [dataConverter parsedMergeData:documentData fieldMask:nil]
  131. : [dataConverter parsedSetData:documentData];
  132. _documentReference.SetData(
  133. std::move(parsed).ToMutations(_documentReference.key(), Precondition::None()), completion);
  134. }
  135. - (void)setData:(NSDictionary<NSString *, id> *)documentData
  136. mergeFields:(NSArray<id> *)mergeFields
  137. completion:(nullable void (^)(NSError *_Nullable error))completion {
  138. ParsedSetData parsed = [self.firestore.dataConverter parsedMergeData:documentData
  139. fieldMask:mergeFields];
  140. _documentReference.SetData(
  141. std::move(parsed).ToMutations(_documentReference.key(), Precondition::None()), completion);
  142. }
  143. - (void)updateData:(NSDictionary<id, id> *)fields {
  144. [self updateData:fields completion:nil];
  145. }
  146. - (void)updateData:(NSDictionary<id, id> *)fields
  147. completion:(nullable void (^)(NSError *_Nullable error))completion {
  148. ParsedUpdateData parsed = [self.firestore.dataConverter parsedUpdateData:fields];
  149. _documentReference.UpdateData(
  150. std::move(parsed).ToMutations(_documentReference.key(), Precondition::Exists(true)),
  151. completion);
  152. }
  153. - (void)deleteDocument {
  154. [self deleteDocumentWithCompletion:nil];
  155. }
  156. - (void)deleteDocumentWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
  157. _documentReference.DeleteDocument(completion);
  158. }
  159. - (void)getDocumentWithCompletion:(FIRDocumentSnapshotBlock)completion {
  160. _documentReference.GetDocument(FIRFirestoreSourceDefault,
  161. [self wrapDocumentSnapshotBlock:completion]);
  162. }
  163. - (void)getDocumentWithSource:(FIRFirestoreSource)source
  164. completion:(FIRDocumentSnapshotBlock)completion {
  165. _documentReference.GetDocument(source, [self wrapDocumentSnapshotBlock:completion]);
  166. }
  167. - (id<FIRListenerRegistration>)addSnapshotListener:(FIRDocumentSnapshotBlock)listener {
  168. return [self addSnapshotListenerWithIncludeMetadataChanges:NO listener:listener];
  169. }
  170. - (id<FIRListenerRegistration>)
  171. addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
  172. listener:(FIRDocumentSnapshotBlock)listener {
  173. ListenOptions options = ListenOptions::FromIncludeMetadataChanges(includeMetadataChanges);
  174. return [self addSnapshotListenerInternalWithOptions:options listener:listener];
  175. }
  176. - (id<FIRListenerRegistration>)addSnapshotListenerInternalWithOptions:(ListenOptions)internalOptions
  177. listener:(FIRDocumentSnapshotBlock)
  178. listener {
  179. ListenerRegistration result = _documentReference.AddSnapshotListener(
  180. std::move(internalOptions), [self wrapDocumentSnapshotBlock:listener]);
  181. return [[FSTListenerRegistration alloc] initWithRegistration:std::move(result)];
  182. }
  183. - (DocumentSnapshot::Listener)wrapDocumentSnapshotBlock:(FIRDocumentSnapshotBlock)block {
  184. class Converter : public EventListener<DocumentSnapshot> {
  185. public:
  186. explicit Converter(FIRDocumentSnapshotBlock block) : block_(block) {
  187. }
  188. void OnEvent(StatusOr<DocumentSnapshot> maybe_snapshot) override {
  189. if (maybe_snapshot.ok()) {
  190. FIRDocumentSnapshot *result =
  191. [[FIRDocumentSnapshot alloc] initWithSnapshot:std::move(maybe_snapshot).ValueOrDie()];
  192. block_(result, nil);
  193. } else {
  194. block_(nil, util::MakeNSError(maybe_snapshot.status()));
  195. }
  196. }
  197. private:
  198. FIRDocumentSnapshotBlock block_;
  199. };
  200. return absl::make_unique<Converter>(block);
  201. }
  202. @end
  203. #pragma mark - FIRDocumentReference (Internal)
  204. @implementation FIRDocumentReference (Internal)
  205. - (const DocumentKey &)key {
  206. return _documentReference.key();
  207. }
  208. @end
  209. NS_ASSUME_NONNULL_END