FIRDocumentReference.h 12 KB

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