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