FIRDocumentReference.mm 9.3 KB

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