FIRDocumentReference.h 12 KB

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