FIRDocumentReference.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.
  79. */
  80. - (void)setData:(NSDictionary<NSString *, id> *)documentData merge:(BOOL)merge;
  81. /**
  82. * Writes to the document referred to by `document` and only replace the fields
  83. * specified under `mergeFields`. Any field that is not specified in `mergeFields`
  84. * is ignored and remains untouched. If the document doesn't yet exist,
  85. * this method creates it and then sets the data.
  86. *
  87. * It is an error to include a field in `mergeFields` that does not have a corresponding
  88. * value in the `data` dictionary.
  89. *
  90. * @param documentData An `NSDictionary` containing the fields that make up the document
  91. * to be written.
  92. * @param mergeFields An `NSArray` that contains a list of `NSString` or `FIRFieldPath` elements
  93. * specifying which fields to merge. Fields can contain dots to reference nested fields within
  94. * the document.
  95. */
  96. - (void)setData:(NSDictionary<NSString *, id> *)documentData mergeFields:(NSArray<id> *)mergeFields;
  97. /**
  98. * Overwrites the document referred to by this `FIRDocumentReference`. If no document exists, it
  99. * is created. If a document already exists, it is overwritten.
  100. *
  101. * @param documentData An `NSDictionary` containing the fields that make up the document
  102. * to be written.
  103. * @param completion A block to execute once the document has been successfully written to the
  104. * server. This block will not be called while the client is offline, though local
  105. * changes will be visible immediately.
  106. */
  107. - (void)setData:(NSDictionary<NSString *, id> *)documentData
  108. completion:(nullable void (^)(NSError *_Nullable error))completion;
  109. /**
  110. * Writes to the document referred to by this DocumentReference. If the document does not yet
  111. * exist, it will be created. If you pass `merge:YES`, the provided data will be merged into
  112. * any existing document.
  113. *
  114. * @param documentData An `NSDictionary` containing the fields that make up the document
  115. * to be written.
  116. * @param merge Whether to merge the provided data into any existing document.
  117. * @param completion A block to execute once the document has been successfully written to the
  118. * server. This block will not be called while the client is offline, though local
  119. * changes will be visible immediately.
  120. */
  121. - (void)setData:(NSDictionary<NSString *, id> *)documentData
  122. merge:(BOOL)merge
  123. completion:(nullable void (^)(NSError *_Nullable error))completion;
  124. /**
  125. * Writes to the document referred to by `document` and only replace the fields
  126. * specified under `mergeFields`. Any field that is not specified in `mergeFields`
  127. * is ignored and remains untouched. If the document doesn't yet exist,
  128. * this method creates it and then sets the data.
  129. *
  130. * It is an error to include a field in `mergeFields` that does not have a corresponding
  131. * value in the `data` dictionary.
  132. *
  133. * @param documentData An `NSDictionary` containing the fields that make up the document
  134. * to be written.
  135. * @param mergeFields An `NSArray` that contains a list of `NSString` or `FIRFieldPath` elements
  136. * specifying which fields to merge. Fields can contain dots to reference nested fields within
  137. * the document.
  138. * @param completion A block to execute once the document has been successfully written to the
  139. * server. This block will not be called while the client is offline, though local
  140. * changes will be visible immediately.
  141. */
  142. - (void)setData:(NSDictionary<NSString *, id> *)documentData
  143. mergeFields:(NSArray<id> *)mergeFields
  144. completion:(nullable void (^)(NSError *_Nullable error))completion;
  145. /**
  146. * Updates fields in the document referred to by this `FIRDocumentReference`.
  147. * If the document does not exist, the update fails (specify a completion block to be notified).
  148. *
  149. * @param fields An `NSDictionary` containing the fields (expressed as an `NSString` or
  150. * `FIRFieldPath`) and values with which to update the document.
  151. */
  152. - (void)updateData:(NSDictionary<id, id> *)fields;
  153. /**
  154. * Updates fields in the document referred to by this `FIRDocumentReference`. If the document
  155. * does not exist, the update fails and the specified completion block receives an error.
  156. *
  157. * @param fields An `NSDictionary` containing the fields (expressed as an `NSString` or
  158. * `FIRFieldPath`) and values with which to update the document.
  159. * @param completion A block to execute when the update is complete. If the update is successful the
  160. * error parameter will be nil, otherwise it will give an indication of how the update failed.
  161. * This block will only execute when the client is online and the commit has completed against
  162. * the server. The completion handler will not be called when the device is offline, though
  163. * local changes will be visible immediately.
  164. */
  165. - (void)updateData:(NSDictionary<id, id> *)fields
  166. completion:(nullable void (^)(NSError *_Nullable error))completion;
  167. // NOTE: this is named 'deleteDocument' because 'delete' is a keyword in Objective-C++.
  168. /** Deletes the document referred to by this `FIRDocumentReference`. */
  169. // clang-format off
  170. - (void)deleteDocument NS_SWIFT_NAME(delete());
  171. // clang-format on
  172. /**
  173. * Deletes the document referred to by this `FIRDocumentReference`.
  174. *
  175. * @param completion A block to execute once the document has been successfully written to the
  176. * server. This block will not be called while the client is offline, though local
  177. * changes will be visible immediately.
  178. */
  179. // clang-format off
  180. - (void)deleteDocumentWithCompletion:(nullable void (^)(NSError *_Nullable error))completion
  181. NS_SWIFT_NAME(delete(completion:));
  182. // clang-format on
  183. #pragma mark - Retrieving Data
  184. /**
  185. * Reads the document referenced by this `FIRDocumentReference`.
  186. *
  187. * This method attempts to provide up-to-date data when possible by waiting for
  188. * data from the server, but it may return cached data or fail if you are
  189. * offline and the server cannot be reached. See the
  190. * `getDocument(source:completion:)` method to change this behavior.
  191. *
  192. * @param completion a block to execute once the document has been successfully read.
  193. */
  194. - (void)getDocumentWithCompletion:(FIRDocumentSnapshotBlock)completion
  195. NS_SWIFT_NAME(getDocument(completion:));
  196. /**
  197. * Reads the document referenced by this `FIRDocumentReference`.
  198. *
  199. * @param source indicates whether the results should be fetched from the cache
  200. * only (`Source.cache`), the server only (`Source.server`), or to attempt
  201. * the server and fall back to the cache (`Source.default`).
  202. * @param completion a block to execute once the document has been successfully read.
  203. */
  204. // clang-format off
  205. - (void)getDocumentWithSource:(FIRFirestoreSource)source
  206. completion:(FIRDocumentSnapshotBlock)completion
  207. NS_SWIFT_NAME(getDocument(source:completion:));
  208. // clang-format on
  209. /**
  210. * Attaches a listener for DocumentSnapshot events.
  211. *
  212. * @param listener The listener to attach.
  213. *
  214. * @return A FIRListenerRegistration that can be used to remove this listener.
  215. */
  216. - (id<FIRListenerRegistration>)addSnapshotListener:(FIRDocumentSnapshotBlock)listener
  217. NS_SWIFT_NAME(addSnapshotListener(_:));
  218. /**
  219. * Attaches a listener for DocumentSnapshot events.
  220. *
  221. * @param includeMetadataChanges Whether metadata-only changes (i.e. only
  222. * `FIRDocumentSnapshot.metadata` changed) should trigger snapshot events.
  223. * @param listener The listener to attach.
  224. *
  225. * @return A FIRListenerRegistration that can be used to remove this listener.
  226. */
  227. // clang-format off
  228. - (id<FIRListenerRegistration>)
  229. addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
  230. listener:(FIRDocumentSnapshotBlock)listener
  231. NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:));
  232. // clang-format on
  233. @end
  234. NS_ASSUME_NONNULL_END