FIRDocumentReference.mm 9.4 KB

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