FIRFirestore.h 14 KB

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