FSTFirestoreClient.h 5.1 KB

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