FIRFirestore.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "FIRListenerRegistration.h"
  18. @class FIRApp;
  19. @class FIRCollectionReference;
  20. @class FIRDocumentReference;
  21. @class FIRFirestoreSettings;
  22. @class FIRQuery;
  23. @class FIRTransaction;
  24. @class FIRWriteBatch;
  25. NS_ASSUME_NONNULL_BEGIN
  26. /**
  27. * `FIRFirestore` represents a Firestore Database and is the entry point for all Firestore
  28. * operations.
  29. */
  30. NS_SWIFT_NAME(Firestore)
  31. @interface FIRFirestore : NSObject
  32. #pragma mark - Initializing
  33. /** :nodoc: */
  34. - (instancetype)init __attribute__((unavailable("Use a static constructor method.")));
  35. /**
  36. * Creates, caches, and returns a `FIRFirestore` using the default `FIRApp`. Each subsequent
  37. * invocation returns the same `FIRFirestore` object.
  38. *
  39. * @return The `FIRFirestore` instance.
  40. */
  41. + (instancetype)firestore NS_SWIFT_NAME(firestore());
  42. /**
  43. * Creates, caches, and returns a `FIRFirestore` object for the specified _app_. Each subsequent
  44. * invocation returns the same `FIRFirestore` object.
  45. *
  46. * @param app The `FIRApp` instance to use for authentication and as a source of the Google Cloud
  47. * Project ID for your Firestore Database. If you want the default instance, you should explicitly
  48. * set it to `[FIRApp defaultApp]`.
  49. *
  50. * @return The `FIRFirestore` instance.
  51. */
  52. + (instancetype)firestoreForApp:(FIRApp *)app NS_SWIFT_NAME(firestore(app:));
  53. /**
  54. * Custom settings used to configure this `FIRFirestore` object.
  55. */
  56. @property(nonatomic, copy) FIRFirestoreSettings *settings;
  57. /**
  58. * The Firebase App associated with this Firestore instance.
  59. */
  60. @property(strong, nonatomic, readonly) FIRApp *app;
  61. #pragma mark - Collections and Documents
  62. /**
  63. * Gets a `FIRCollectionReference` referring to the collection at the specified path within the
  64. * database.
  65. *
  66. * @param collectionPath The slash-separated path of the collection for which to get a
  67. * `FIRCollectionReference`.
  68. *
  69. * @return The `FIRCollectionReference` at the specified _collectionPath_.
  70. */
  71. - (FIRCollectionReference *)collectionWithPath:(NSString *)collectionPath
  72. NS_SWIFT_NAME(collection(_:));
  73. /**
  74. * Gets a `FIRDocumentReference` referring to the document at the specified path within the
  75. * database.
  76. *
  77. * @param documentPath The slash-separated path of the document for which to get a
  78. * `FIRDocumentReference`.
  79. *
  80. * @return The `FIRDocumentReference` for the specified _documentPath_.
  81. */
  82. - (FIRDocumentReference *)documentWithPath:(NSString *)documentPath NS_SWIFT_NAME(document(_:));
  83. #pragma mark - Collection Group Queries
  84. /**
  85. * Creates and returns a new `Query` that includes all documents in the database that are contained
  86. * in a collection or subcollection with the given collectionID.
  87. *
  88. * @param collectionID Identifies the collections to query over. Every collection or subcollection
  89. * with this ID as the last segment of its path will be included. Cannot contain a slash.
  90. * @return The created `Query`.
  91. */
  92. - (FIRQuery *)collectionGroupWithID:(NSString *)collectionID NS_SWIFT_NAME(collectionGroup(_:));
  93. #pragma mark - Transactions and Write Batches
  94. /**
  95. * Executes the given updateBlock and then attempts to commit the changes applied within an atomic
  96. * transaction.
  97. *
  98. * The maximum number of writes allowed in a single transaction is 500, but note that each usage of
  99. * `FieldValue.serverTimestamp()`, `FieldValue.arrayUnion()`, `FieldValue.arrayRemove()`, or
  100. * `FieldValue.increment()` inside a transaction counts as an additional write.
  101. *
  102. * In the updateBlock, a set of reads and writes can be performed atomically using the
  103. * `FIRTransaction` object passed to the block. After the updateBlock is run, Firestore will attempt
  104. * to apply the changes to the server. If any of the data read has been modified outside of this
  105. * transaction since being read, then the transaction will be retried by executing the updateBlock
  106. * again. If the transaction still fails after 5 retries, then the transaction will fail.
  107. *
  108. * Since the updateBlock may be executed multiple times, it should avoiding doing anything that
  109. * would cause side effects.
  110. *
  111. * Any value maybe be returned from the updateBlock. If the transaction is successfully committed,
  112. * then the completion block will be passed that value. The updateBlock also has an `NSError` out
  113. * parameter. If this is set, then the transaction will not attempt to commit, and the given error
  114. * will be passed to the completion block.
  115. *
  116. * The `FIRTransaction` object passed to the updateBlock contains methods for accessing documents
  117. * and collections. Unlike other firestore access, data accessed with the transaction will not
  118. * reflect local changes that have not been committed. For this reason, it is required that all
  119. * reads are performed before any writes. Transactions must be performed while online. Otherwise,
  120. * reads will fail, the final commit will fail, and the completion block will return an error.
  121. *
  122. * @param updateBlock The block to execute within the transaction context.
  123. * @param completion The block to call with the result or error of the transaction. This
  124. * block will run even if the client is offline, unless the process is killed.
  125. */
  126. - (void)runTransactionWithBlock:(id _Nullable (^)(FIRTransaction *, NSError **))updateBlock
  127. completion:(void (^)(id _Nullable result, NSError *_Nullable error))completion;
  128. /**
  129. * Creates a write batch, used for performing multiple writes as a single
  130. * atomic operation.
  131. *
  132. * The maximum number of writes allowed in a single batch is 500, but note that each usage of
  133. * `FieldValue.serverTimestamp()`, `FieldValue.arrayUnion()`, `FieldValue.arrayRemove()`, or
  134. * `FieldValue.increment()` inside a batch counts as an additional write.
  135. * Unlike transactions, write batches are persisted offline and therefore are preferable when you
  136. * don't need to condition your writes on read data.
  137. */
  138. - (FIRWriteBatch *)batch;
  139. #pragma mark - Logging
  140. /** Enables or disables logging from the Firestore client. */
  141. + (void)enableLogging:(BOOL)logging;
  142. #pragma mark - Network
  143. /**
  144. * Re-enables usage of the network by this Firestore instance after a prior call to
  145. * `disableNetworkWithCompletion`. Completion block, if provided, will be called once network uasge
  146. * has been enabled.
  147. */
  148. - (void)enableNetworkWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
  149. /**
  150. * Disables usage of the network by this Firestore instance. It can be re-enabled by via
  151. * `enableNetworkWithCompletion`. While the network is disabled, any snapshot listeners or get calls
  152. * will return results from cache and any write operations will be queued until the network is
  153. * restored. The completion block, if provided, will be called once network usage has been disabled.
  154. */
  155. - (void)disableNetworkWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
  156. /**
  157. * Clears the persistent storage. This includes pending writes and cached documents.
  158. *
  159. * Must be called while the firestore instance is not started (after the app is shutdown or when
  160. * the app is first initialized). On startup, this method must be called before other methods
  161. * (other than `FIRFirestore.settings`). If the firestore instance is still running, the function
  162. * will complete with an error code of `FailedPrecondition`.
  163. *
  164. * Note: `clearPersistence(completion:)` is primarily intended to help write reliable tests that
  165. * use Firestore. It uses the most efficient mechanism possible for dropping existing data but
  166. * does not attempt to securely overwrite or otherwise make cached data unrecoverable. For
  167. * applications that are sensitive to the disclosure of cache data in between user sessions we
  168. * strongly recommend not to enable persistence in the first place.
  169. */
  170. - (void)clearPersistenceWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
  171. /**
  172. * Waits until all currently pending writes for the active user have been acknowledged by the
  173. * backend.
  174. *
  175. * The completion block is called immediately without error if there are no outstanding writes.
  176. * Otherwise, the completion block is called when all previously issued writes (including those
  177. * written in a previous app session) have been acknowledged by the backend. The completion
  178. * block does not wait for writes that were added after the method is called. If you
  179. * wish to wait for additional writes, you have to call `waitForPendingWritesWithCompletion`
  180. * again.
  181. *
  182. * Any outstanding `waitForPendingWritesWithCompletion` completion blocks are called with an
  183. * error during user change.
  184. */
  185. - (void)waitForPendingWritesWithCompletion:(void (^)(NSError *_Nullable error))completion;
  186. /**
  187. * Attaches a listener for a snapshots-in-sync event. The snapshots-in-sync event indicates that all
  188. * listeners affected by a given change have fired, even if a single server-generated change affects
  189. * multiple listeners.
  190. *
  191. * NOTE: The snapshots-in-sync event only indicates that listeners are in sync with each other, but
  192. * does not relate to whether those snapshots are in sync with the server. Use SnapshotMetadata in
  193. * the individual listeners to determine if a snapshot is from the cache or the server.
  194. *
  195. * @param listener A callback to be called every time all snapshot listeners are in sync with each
  196. * other.
  197. * @return A FIRListenerRegistration object that can be used to remove the listener.
  198. */
  199. - (id<FIRListenerRegistration>)addSnapshotsInSyncListener:(void (^)(void))listener
  200. NS_SWIFT_NAME(addSnapshotsInSyncListener(_:));
  201. #pragma mark - Terminating
  202. /**
  203. * Terminates this `FIRFirestore` instance.
  204. *
  205. * After calling `terminate` only the `clearPersistence` method may be used. Any other method will
  206. * throw an error.
  207. *
  208. * To restart after termination, simply create a new instance of FIRFirestore with `firestore` or
  209. * `firestoreForApp` methods.
  210. *
  211. * Termination does not cancel any pending writes and any tasks that are awaiting a response from
  212. * the server will not be resolved. The next time you start this instance, it will resume attempting
  213. * to send these writes to the server.
  214. *
  215. * Note: Under normal circumstances, calling this method is not required. This method is useful only
  216. * when you want to force this instance to release all of its resources or in combination with
  217. * `clearPersistence` to ensure that all local state is destroyed between test runs.
  218. *
  219. * @param completion A block to execute once everything has been terminated.
  220. */
  221. - (void)terminateWithCompletion:(nullable void (^)(NSError *_Nullable error))completion
  222. NS_SWIFT_NAME(terminate(completion:));
  223. @end
  224. NS_ASSUME_NONNULL_END