FIRDocumentReference.h 11 KB

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