FIRDocumentReference.h 12 KB

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