FIRDocumentReference.mm 9.4 KB

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