FIRDocumentReference.mm 9.9 KB

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