FIRDocumentReference.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <Foundation/Foundation.h>
  17. #import "FIRFirestoreSource.h"
  18. #import "FIRListenerRegistration.h"
  19. @class FIRCollectionReference;
  20. @class FIRDocumentSnapshot;
  21. @class FIRFirestore;
  22. NS_ASSUME_NONNULL_BEGIN
  23. typedef void (^FIRDocumentSnapshotBlock)(FIRDocumentSnapshot *_Nullable snapshot,
  24. NSError *_Nullable error);
  25. /**
  26. * A `FIRDocumentReference` refers to a document location in a Firestore database and can be
  27. * used to write, read, or listen to the location. The document at the referenced location
  28. * may or may not exist. A `FIRDocumentReference` can also be used to create a
  29. * `FIRCollectionReference` to a subcollection.
  30. */
  31. NS_SWIFT_NAME(DocumentReference)
  32. @interface FIRDocumentReference : NSObject
  33. /** :nodoc: */
  34. - (instancetype)init
  35. __attribute__((unavailable("FIRDocumentReference cannot be created directly.")));
  36. /** The ID of the document referred to. */
  37. @property(nonatomic, strong, readonly) NSString *documentID;
  38. /** A reference to the collection to which this `DocumentReference` belongs. */
  39. @property(nonatomic, strong, readonly) FIRCollectionReference *parent;
  40. /** The `FIRFirestore` for the Firestore database (useful for performing transactions, etc.). */
  41. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  42. /**
  43. * A string representing the path of the referenced document (relative to the root of the
  44. * database).
  45. */
  46. @property(nonatomic, strong, readonly) NSString *path;
  47. /**
  48. * Gets a `FIRCollectionReference` referring to the collection at the specified
  49. * path, relative to this document.
  50. *
  51. * @param collectionPath The slash-separated relative path of the collection for which to get a
  52. * `FIRCollectionReference`.
  53. *
  54. * @return The `FIRCollectionReference` at the specified _collectionPath_.
  55. */
  56. - (FIRCollectionReference *)collectionWithPath:(NSString *)collectionPath
  57. NS_SWIFT_NAME(collection(_:));
  58. #pragma mark - Writing Data
  59. /**
  60. * Writes to the document referred to by `FIRDocumentReference`. If the document doesn't yet exist,
  61. * this method creates it and then sets the data. If the document exists, this method overwrites
  62. * the document data with the new values.
  63. *
  64. * @param documentData An `NSDictionary` that contains the fields and data to write to the
  65. * document.
  66. */
  67. - (void)setData:(NSDictionary<NSString *, id> *)documentData;
  68. /**
  69. * Writes to the document referred to by this DocumentReference. If the document does not yet
  70. * exist, it will be created. If you pass `merge:YES`, the provided data will be merged into
  71. * any existing document.
  72. *
  73. * @param documentData An `NSDictionary` that contains the fields and data to write to the
  74. * document.
  75. * @param merge Whether to merge the provided data into any existing document.
  76. */
  77. - (void)setData:(NSDictionary<NSString *, id> *)documentData merge:(BOOL)merge;
  78. /**
  79. * Writes to the document referred to by `document` and only replace the fields
  80. * specified under `mergeFields`. Any field that is not specified in `mergeFields`
  81. * is ignored and remains untouched. If the document doesn't yet exist,
  82. * this method creates it and then sets the data.
  83. *
  84. * It is an error to include a field in `mergeFields` that does not have a corresponding
  85. * value in the `data` dictionary.
  86. *
  87. * @param documentData An `NSDictionary` containing the fields that make up the document
  88. * to be written.
  89. * @param mergeFields An `NSArray` that contains a list of `NSString` or `FIRFieldPath` elements
  90. * specifying which fields to merge. Fields can contain dots to reference nested fields within
  91. * the document.
  92. */
  93. - (void)setData:(NSDictionary<NSString *, id> *)documentData mergeFields:(NSArray<id> *)mergeFields;
  94. /**
  95. * Overwrites the document referred to by this `FIRDocumentReference`. If no document exists, it
  96. * is created. If a document already exists, it is overwritten.
  97. *
  98. * @param documentData An `NSDictionary` containing the fields that make up the document
  99. * to be written.
  100. * @param completion A block to execute once the document has been successfully written to the
  101. * server. This block will not be called while the client is offline, though local
  102. * changes will be visible immediately.
  103. */
  104. - (void)setData:(NSDictionary<NSString *, id> *)documentData
  105. completion:(nullable void (^)(NSError *_Nullable error))completion;
  106. /**
  107. * Writes to the document referred to by this DocumentReference. If the document does not yet
  108. * exist, it will be created. If you pass `merge:YES`, the provided data will be merged into
  109. * any existing document.
  110. *
  111. * @param documentData An `NSDictionary` containing the fields that make up the document
  112. * to be written.
  113. * @param merge Whether to merge the provided data into any existing document.
  114. * @param completion A block to execute once the document has been successfully written to the
  115. * server. This block will not be called while the client is offline, though local
  116. * changes will be visible immediately.
  117. */
  118. - (void)setData:(NSDictionary<NSString *, id> *)documentData
  119. merge:(BOOL)merge
  120. completion:(nullable void (^)(NSError *_Nullable error))completion;
  121. /**
  122. * Writes to the document referred to by `document` and only replace the fields
  123. * specified under `mergeFields`. Any field that is not specified in `mergeFields`
  124. * is ignored and remains untouched. If the document doesn't yet exist,
  125. * this method creates it and then sets the data.
  126. *
  127. * It is an error to include a field in `mergeFields` that does not have a corresponding
  128. * value in the `data` dictionary.
  129. *
  130. * @param documentData An `NSDictionary` containing the fields that make up the document
  131. * to be written.
  132. * @param mergeFields An `NSArray` that contains a list of `NSString` or `FIRFieldPath` elements
  133. * specifying which fields to merge. Fields can contain dots to reference nested fields within
  134. * the document.
  135. * @param completion A block to execute once the document has been successfully written to the
  136. * server. This block will not be called while the client is offline, though local
  137. * changes will be visible immediately.
  138. */
  139. - (void)setData:(NSDictionary<NSString *, id> *)documentData
  140. mergeFields:(NSArray<id> *)mergeFields
  141. completion:(nullable void (^)(NSError *_Nullable error))completion;
  142. /**
  143. * Updates fields in the document referred to by this `FIRDocumentReference`.
  144. * If the document does not exist, the update fails (specify a completion block to be notified).
  145. *
  146. * @param fields An `NSDictionary` containing the fields (expressed as an `NSString` or
  147. * `FIRFieldPath`) and values with which to update the document.
  148. */
  149. - (void)updateData:(NSDictionary<id, id> *)fields;
  150. /**
  151. * Updates fields in the document referred to by this `FIRDocumentReference`. If the document
  152. * does not exist, the update fails and the specified completion block receives an error.
  153. *
  154. * @param fields An `NSDictionary` containing the fields (expressed as an `NSString` or
  155. * `FIRFieldPath`) and values with which to update the document.
  156. * @param completion A block to execute when the update is complete. If the update is successful the
  157. * error parameter will be nil, otherwise it will give an indication of how the update failed.
  158. * This block will only execute when the client is online and the commit has completed against
  159. * the server. The completion handler will not be called when the device is offline, though
  160. * local changes will be visible immediately.
  161. */
  162. - (void)updateData:(NSDictionary<id, id> *)fields
  163. completion:(nullable void (^)(NSError *_Nullable error))completion;
  164. // NOTE: this is named 'deleteDocument' because 'delete' is a keyword in Objective-C++.
  165. /** Deletes the document referred to by this `FIRDocumentReference`. */
  166. // clang-format off
  167. - (void)deleteDocument NS_SWIFT_NAME(delete());
  168. // clang-format on
  169. /**
  170. * Deletes the document referred to by this `FIRDocumentReference`.
  171. *
  172. * @param completion A block to execute once the document has been successfully written to the
  173. * server. This block will not be called while the client is offline, though local
  174. * changes will be visible immediately.
  175. */
  176. // clang-format off
  177. - (void)deleteDocumentWithCompletion:(nullable void (^)(NSError *_Nullable error))completion
  178. NS_SWIFT_NAME(delete(completion:));
  179. // clang-format on
  180. #pragma mark - Retrieving Data
  181. /**
  182. * Reads the document referenced by this `FIRDocumentReference`.
  183. *
  184. * This method attempts to provide up-to-date data when possible by waiting for
  185. * data from the server, but it may return cached data or fail if you are
  186. * offline and the server cannot be reached. See the
  187. * `getDocument(source:completion:)` method to change this behavior.
  188. *
  189. * @param completion a block to execute once the document has been successfully read.
  190. */
  191. - (void)getDocumentWithCompletion:(FIRDocumentSnapshotBlock)completion
  192. NS_SWIFT_NAME(getDocument(completion:));
  193. /**
  194. * Reads the document referenced by this `FIRDocumentReference`.
  195. *
  196. * @param source indicates whether the results should be fetched from the cache
  197. * only (`Source.cache`), the server only (`Source.server`), or to attempt
  198. * the server and fall back to the cache (`Source.default`).
  199. * @param completion a block to execute once the document has been successfully read.
  200. */
  201. // clang-format off
  202. - (void)getDocumentWithSource:(FIRFirestoreSource)source
  203. completion:(FIRDocumentSnapshotBlock)completion
  204. NS_SWIFT_NAME(getDocument(source:completion:));
  205. // clang-format on
  206. /**
  207. * Attaches a listener for DocumentSnapshot events.
  208. *
  209. * @param listener The listener to attach.
  210. *
  211. * @return A FIRListenerRegistration that can be used to remove this listener.
  212. */
  213. - (id<FIRListenerRegistration>)addSnapshotListener:(FIRDocumentSnapshotBlock)listener
  214. NS_SWIFT_NAME(addSnapshotListener(_:));
  215. /**
  216. * Attaches a listener for DocumentSnapshot events.
  217. *
  218. * @param includeMetadataChanges Whether metadata-only changes (i.e. only
  219. * `FIRDocumentSnapshot.metadata` changed) should trigger snapshot events.
  220. * @param listener The listener to attach.
  221. *
  222. * @return A FIRListenerRegistration that can be used to remove this listener.
  223. */
  224. // clang-format off
  225. - (id<FIRListenerRegistration>)
  226. addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
  227. listener:(FIRDocumentSnapshotBlock)listener
  228. NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:));
  229. // clang-format on
  230. @end
  231. NS_ASSUME_NONNULL_END