FSTFirestoreClient.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include <memory>
  18. #include <vector>
  19. #import "Firestore/Source/Core/FSTTypes.h"
  20. #include "Firestore/core/src/firebase/firestore/api/document_reference.h"
  21. #include "Firestore/core/src/firebase/firestore/api/document_snapshot.h"
  22. #include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
  23. #include "Firestore/core/src/firebase/firestore/core/database_info.h"
  24. #include "Firestore/core/src/firebase/firestore/core/listen_options.h"
  25. #include "Firestore/core/src/firebase/firestore/core/query_listener.h"
  26. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  27. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  28. #include "Firestore/core/src/firebase/firestore/util/async_queue.h"
  29. #include "Firestore/core/src/firebase/firestore/util/executor.h"
  30. #include "Firestore/core/src/firebase/firestore/util/statusor_callback.h"
  31. @class FIRDocumentReference;
  32. @class FIRDocumentSnapshot;
  33. @class FIRFirestoreSettings;
  34. @class FIRQuery;
  35. @class FIRQuerySnapshot;
  36. @class FSTDatabaseID;
  37. @class FSTDatabaseInfo;
  38. @class FSTDocument;
  39. @class FSTMutation;
  40. @class FSTQuery;
  41. @class FSTTransaction;
  42. NS_ASSUME_NONNULL_BEGIN
  43. using firebase::firestore::api::DocumentSnapshot;
  44. using firebase::firestore::core::ListenOptions;
  45. using firebase::firestore::core::QueryListener;
  46. using firebase::firestore::core::ViewSnapshot;
  47. /**
  48. * FirestoreClient is a top-level class that constructs and owns all of the pieces of the client
  49. * SDK architecture. It is responsible for creating the worker queue that is shared by all of the
  50. * other components in the system.
  51. */
  52. @interface FSTFirestoreClient : NSObject
  53. /**
  54. * Creates and returns a FSTFirestoreClient with the given parameters.
  55. *
  56. * All callbacks and events will be triggered on the provided userExecutor.
  57. */
  58. + (instancetype)
  59. clientWithDatabaseInfo:(const firebase::firestore::core::DatabaseInfo &)databaseInfo
  60. settings:(FIRFirestoreSettings *)settings
  61. credentialsProvider:(firebase::firestore::auth::CredentialsProvider *)
  62. credentialsProvider // no passing ownership
  63. userExecutor:(std::unique_ptr<firebase::firestore::util::Executor>)userExecutor
  64. workerQueue:(std::unique_ptr<firebase::firestore::util::AsyncQueue>)workerQueue;
  65. - (instancetype)init __attribute__((unavailable("Use static constructor method.")));
  66. /** Shuts down this client, cancels all writes / listeners, and releases all resources. */
  67. - (void)shutdownWithCompletion:(nullable FSTVoidErrorBlock)completion;
  68. /** Disables the network connection. Pending operations will not complete. */
  69. - (void)disableNetworkWithCompletion:(nullable FSTVoidErrorBlock)completion;
  70. /** Enables the network connection and requeues all pending operations. */
  71. - (void)enableNetworkWithCompletion:(nullable FSTVoidErrorBlock)completion;
  72. /** Starts listening to a query. */
  73. - (std::shared_ptr<QueryListener>)listenToQuery:(FSTQuery *)query
  74. options:(ListenOptions)options
  75. listener:(ViewSnapshot::SharedListener &&)listener;
  76. /** Stops listening to a query previously listened to. */
  77. - (void)removeListener:(const std::shared_ptr<QueryListener> &)listener;
  78. /**
  79. * Retrieves a document from the cache via the indicated completion. If the doc
  80. * doesn't exist, an error will be sent to the completion.
  81. */
  82. - (void)getDocumentFromLocalCache:(const firebase::firestore::api::DocumentReference &)doc
  83. completion:(DocumentSnapshot::Listener &&)completion;
  84. /**
  85. * Retrieves a (possibly empty) set of documents from the cache via the
  86. * indicated completion.
  87. */
  88. - (void)getDocumentsFromLocalCache:(FIRQuery *)query
  89. completion:(void (^)(FIRQuerySnapshot *_Nullable query,
  90. NSError *_Nullable error))completion;
  91. /** Write mutations. completion will be notified when it's written to the backend. */
  92. - (void)writeMutations:(std::vector<FSTMutation *> &&)mutations
  93. completion:(nullable FSTVoidErrorBlock)completion;
  94. /** Tries to execute the transaction in updateBlock up to retries times. */
  95. - (void)transactionWithRetries:(int)retries
  96. updateBlock:(FSTTransactionBlock)updateBlock
  97. completion:(FSTVoidIDErrorBlock)completion;
  98. /** The database ID of the databaseInfo this client was initialized with. */
  99. // Ownes a DatabaseInfo instance, which contains the id here.
  100. @property(nonatomic, assign, readonly) const firebase::firestore::model::DatabaseId *databaseID;
  101. /**
  102. * Dispatch queue for user callbacks / events. This will often be the "Main Dispatch Queue" of the
  103. * app but the developer can configure it to a different queue if they so choose.
  104. */
  105. - (firebase::firestore::util::Executor *)userExecutor;
  106. /** For testing only. */
  107. - (firebase::firestore::util::AsyncQueue *)workerQueue;
  108. @end
  109. NS_ASSUME_NONNULL_END