FSTFirestoreClient.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/api/query_core.h"
  23. #include "Firestore/core/src/firebase/firestore/api/settings.h"
  24. #include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
  25. #include "Firestore/core/src/firebase/firestore/core/database_info.h"
  26. #include "Firestore/core/src/firebase/firestore/core/listen_options.h"
  27. #include "Firestore/core/src/firebase/firestore/core/query_listener.h"
  28. #include "Firestore/core/src/firebase/firestore/core/transaction.h"
  29. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  30. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  31. #include "Firestore/core/src/firebase/firestore/util/async_queue.h"
  32. #include "Firestore/core/src/firebase/firestore/util/executor.h"
  33. #include "Firestore/core/src/firebase/firestore/util/statusor_callback.h"
  34. @class FIRDocumentReference;
  35. @class FIRDocumentSnapshot;
  36. @class FIRQuery;
  37. @class FIRQuerySnapshot;
  38. @class FSTDatabaseID;
  39. @class FSTDatabaseInfo;
  40. @class FSTMutation;
  41. @class FSTQuery;
  42. @class FSTTransaction;
  43. namespace api = firebase::firestore::api;
  44. namespace auth = firebase::firestore::auth;
  45. namespace core = firebase::firestore::core;
  46. namespace model = firebase::firestore::model;
  47. namespace util = firebase::firestore::util;
  48. NS_ASSUME_NONNULL_BEGIN
  49. /**
  50. * FirestoreClient is a top-level class that constructs and owns all of the pieces of the client
  51. * SDK architecture. It is responsible for creating the worker queue that is shared by all of the
  52. * other components in the system.
  53. */
  54. @interface FSTFirestoreClient : NSObject
  55. /**
  56. * Creates and returns a FSTFirestoreClient with the given parameters.
  57. *
  58. * All callbacks and events will be triggered on the provided userExecutor.
  59. */
  60. + (instancetype)clientWithDatabaseInfo:(const core::DatabaseInfo &)databaseInfo
  61. settings:(const api::Settings &)settings
  62. credentialsProvider:
  63. (auth::CredentialsProvider *)credentialsProvider // no passing ownership
  64. userExecutor:(std::shared_ptr<util::Executor>)userExecutor
  65. workerQueue:(std::shared_ptr<util::AsyncQueue>)workerQueue;
  66. - (instancetype)init NS_UNAVAILABLE;
  67. /** Shuts down this client, cancels all writes / listeners, and releases all resources. */
  68. - (void)shutdownWithCallback:(util::StatusCallback)callback;
  69. /** Disables the network connection. Pending operations will not complete. */
  70. - (void)disableNetworkWithCallback:(util::StatusCallback)callback;
  71. /** Enables the network connection and requeues all pending operations. */
  72. - (void)enableNetworkWithCallback:(util::StatusCallback)callback;
  73. /** Starts listening to a query. */
  74. - (std::shared_ptr<core::QueryListener>)listenToQuery:(FSTQuery *)query
  75. options:(core::ListenOptions)options
  76. listener:
  77. (core::ViewSnapshot::SharedListener &&)listener;
  78. /** Stops listening to a query previously listened to. */
  79. - (void)removeListener:(const std::shared_ptr<core::QueryListener> &)listener;
  80. /**
  81. * Retrieves a document from the cache via the indicated callback. If the doc
  82. * doesn't exist, an error will be sent to the callback.
  83. */
  84. - (void)getDocumentFromLocalCache:(const api::DocumentReference &)doc
  85. callback:(api::DocumentSnapshot::Listener &&)callback;
  86. /**
  87. * Retrieves a (possibly empty) set of documents from the cache via the
  88. * indicated completion.
  89. */
  90. - (void)getDocumentsFromLocalCache:(const api::Query &)query
  91. callback:(api::QuerySnapshot::Listener &&)callback;
  92. /** Write mutations. callback will be notified when it's written to the backend. */
  93. - (void)writeMutations:(std::vector<FSTMutation *> &&)mutations
  94. callback:(util::StatusCallback)callback;
  95. /** Tries to execute the transaction in updateCallback up to retries times. */
  96. - (void)transactionWithRetries:(int)retries
  97. updateCallback:(core::TransactionUpdateCallback)updateCallback
  98. resultCallback:(core::TransactionResultCallback)resultCallback;
  99. /** The database ID of the databaseInfo this client was initialized with. */
  100. @property(nonatomic, assign, readonly) const 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. - (const std::shared_ptr<util::Executor> &)userExecutor;
  106. /** For testing only. */
  107. - (const std::shared_ptr<util::AsyncQueue> &)workerQueue;
  108. - (bool)isShutdown;
  109. @end
  110. NS_ASSUME_NONNULL_END