FIRDocumentReference.mm 9.4 KB

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