| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- /*
- * Copyright 2017 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import "Firestore/Source/Core/FSTFirestoreClient.h"
- #import "Firestore/Source/Auth/FSTCredentialsProvider.h"
- #import "Firestore/Source/Core/FSTDatabaseInfo.h"
- #import "Firestore/Source/Core/FSTEventManager.h"
- #import "Firestore/Source/Core/FSTSyncEngine.h"
- #import "Firestore/Source/Core/FSTTransaction.h"
- #import "Firestore/Source/Local/FSTEagerGarbageCollector.h"
- #import "Firestore/Source/Local/FSTLevelDB.h"
- #import "Firestore/Source/Local/FSTLocalSerializer.h"
- #import "Firestore/Source/Local/FSTLocalStore.h"
- #import "Firestore/Source/Local/FSTMemoryPersistence.h"
- #import "Firestore/Source/Local/FSTNoOpGarbageCollector.h"
- #import "Firestore/Source/Remote/FSTDatastore.h"
- #import "Firestore/Source/Remote/FSTRemoteStore.h"
- #import "Firestore/Source/Remote/FSTSerializerBeta.h"
- #import "Firestore/Source/Util/FSTAssert.h"
- #import "Firestore/Source/Util/FSTClasses.h"
- #import "Firestore/Source/Util/FSTDispatchQueue.h"
- #import "Firestore/Source/Util/FSTLogger.h"
- NS_ASSUME_NONNULL_BEGIN
- @interface FSTFirestoreClient ()
- - (instancetype)initWithDatabaseInfo:(FSTDatabaseInfo *)databaseInfo
- usePersistence:(BOOL)usePersistence
- credentialsProvider:(id<FSTCredentialsProvider>)credentialsProvider
- userDispatchQueue:(FSTDispatchQueue *)userDispatchQueue
- workerDispatchQueue:(FSTDispatchQueue *)queue NS_DESIGNATED_INITIALIZER;
- @property(nonatomic, strong, readonly) FSTDatabaseInfo *databaseInfo;
- @property(nonatomic, strong, readonly) FSTEventManager *eventManager;
- @property(nonatomic, strong, readonly) id<FSTPersistence> persistence;
- @property(nonatomic, strong, readonly) FSTSyncEngine *syncEngine;
- @property(nonatomic, strong, readonly) FSTRemoteStore *remoteStore;
- @property(nonatomic, strong, readonly) FSTLocalStore *localStore;
- /**
- * Dispatch queue responsible for all of our internal processing. When we get incoming work from
- * the user (via public API) or the network (incoming GRPC messages), we should always dispatch
- * onto this queue. This ensures our internal data structures are never accessed from multiple
- * threads simultaneously.
- */
- @property(nonatomic, strong, readonly) FSTDispatchQueue *workerDispatchQueue;
- @property(nonatomic, strong, readonly) id<FSTCredentialsProvider> credentialsProvider;
- @end
- @implementation FSTFirestoreClient
- + (instancetype)clientWithDatabaseInfo:(FSTDatabaseInfo *)databaseInfo
- usePersistence:(BOOL)usePersistence
- credentialsProvider:(id<FSTCredentialsProvider>)credentialsProvider
- userDispatchQueue:(FSTDispatchQueue *)userDispatchQueue
- workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue {
- return [[FSTFirestoreClient alloc] initWithDatabaseInfo:databaseInfo
- usePersistence:usePersistence
- credentialsProvider:credentialsProvider
- userDispatchQueue:userDispatchQueue
- workerDispatchQueue:workerDispatchQueue];
- }
- - (instancetype)initWithDatabaseInfo:(FSTDatabaseInfo *)databaseInfo
- usePersistence:(BOOL)usePersistence
- credentialsProvider:(id<FSTCredentialsProvider>)credentialsProvider
- userDispatchQueue:(FSTDispatchQueue *)userDispatchQueue
- workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue {
- if (self = [super init]) {
- _databaseInfo = databaseInfo;
- _credentialsProvider = credentialsProvider;
- _userDispatchQueue = userDispatchQueue;
- _workerDispatchQueue = workerDispatchQueue;
- dispatch_semaphore_t initialUserAvailable = dispatch_semaphore_create(0);
- __block FSTUser *initialUser;
- FSTWeakify(self);
- _credentialsProvider.userChangeListener = ^(FSTUser *user) {
- FSTStrongify(self);
- if (self) {
- if (!initialUser) {
- initialUser = user;
- dispatch_semaphore_signal(initialUserAvailable);
- } else {
- [workerDispatchQueue dispatchAsync:^{
- [self userDidChange:user];
- }];
- }
- }
- };
- // Defer initialization until we get the current user from the userChangeListener. This is
- // guaranteed to be synchronously dispatched onto our worker queue, so we will be initialized
- // before any subsequently queued work runs.
- [_workerDispatchQueue dispatchAsync:^{
- dispatch_semaphore_wait(initialUserAvailable, DISPATCH_TIME_FOREVER);
- [self initializeWithUser:initialUser usePersistence:usePersistence];
- }];
- }
- return self;
- }
- - (void)initializeWithUser:(FSTUser *)user usePersistence:(BOOL)usePersistence {
- // Do all of our initialization on our own dispatch queue.
- [self.workerDispatchQueue verifyIsCurrentQueue];
- // Note: The initialization work must all be synchronous (we can't dispatch more work) since
- // external write/listen operations could get queued to run before that subsequent work
- // completes.
- id<FSTGarbageCollector> garbageCollector;
- if (usePersistence) {
- // TODO(http://b/33384523): For now we just disable garbage collection when persistence is
- // enabled.
- garbageCollector = [[FSTNoOpGarbageCollector alloc] init];
- NSString *dir = [FSTLevelDB storageDirectoryForDatabaseInfo:self.databaseInfo
- documentsDirectory:[FSTLevelDB documentsDirectory]];
- FSTSerializerBeta *remoteSerializer =
- [[FSTSerializerBeta alloc] initWithDatabaseID:self.databaseInfo.databaseID];
- FSTLocalSerializer *serializer =
- [[FSTLocalSerializer alloc] initWithRemoteSerializer:remoteSerializer];
- _persistence = [[FSTLevelDB alloc] initWithDirectory:dir serializer:serializer];
- } else {
- garbageCollector = [[FSTEagerGarbageCollector alloc] init];
- _persistence = [FSTMemoryPersistence persistence];
- }
- NSError *error;
- if (![_persistence start:&error]) {
- // If local storage fails to start then just throw up our hands: the error is unrecoverable.
- // There's nothing an end-user can do and nearly all failures indicate the developer is doing
- // something grossly wrong so we should stop them cold in their tracks with a failure they
- // can't ignore.
- [NSException raise:NSInternalInconsistencyException format:@"Failed to open DB: %@", error];
- }
- _localStore = [[FSTLocalStore alloc] initWithPersistence:_persistence
- garbageCollector:garbageCollector
- initialUser:user];
- FSTDatastore *datastore = [FSTDatastore datastoreWithDatabase:self.databaseInfo
- workerDispatchQueue:self.workerDispatchQueue
- credentials:self.credentialsProvider];
- _remoteStore = [FSTRemoteStore remoteStoreWithLocalStore:_localStore datastore:datastore];
- _syncEngine = [[FSTSyncEngine alloc] initWithLocalStore:_localStore
- remoteStore:_remoteStore
- initialUser:user];
- _eventManager = [FSTEventManager eventManagerWithSyncEngine:_syncEngine];
- // Setup wiring for remote store.
- _remoteStore.syncEngine = _syncEngine;
- _remoteStore.onlineStateDelegate = _eventManager;
- // NOTE: RemoteStore depends on LocalStore (for persisting stream tokens, refilling mutation
- // queue, etc.) so must be started after LocalStore.
- [_localStore start];
- [_remoteStore start];
- }
- - (void)userDidChange:(FSTUser *)user {
- [self.workerDispatchQueue verifyIsCurrentQueue];
- FSTLog(@"User Changed: %@", user);
- [self.syncEngine userDidChange:user];
- }
- - (void)disableNetworkWithCompletion:(nullable FSTVoidErrorBlock)completion {
- [self.workerDispatchQueue dispatchAsync:^{
- [self.remoteStore disableNetwork];
- if (completion) {
- [self.userDispatchQueue dispatchAsync:^{
- completion(nil);
- }];
- }
- }];
- }
- - (void)enableNetworkWithCompletion:(nullable FSTVoidErrorBlock)completion {
- [self.workerDispatchQueue dispatchAsync:^{
- [self.remoteStore enableNetwork];
- if (completion) {
- [self.userDispatchQueue dispatchAsync:^{
- completion(nil);
- }];
- }
- }];
- }
- - (void)shutdownWithCompletion:(nullable FSTVoidErrorBlock)completion {
- [self.workerDispatchQueue dispatchAsync:^{
- self.credentialsProvider.userChangeListener = nil;
- [self.remoteStore shutdown];
- [self.localStore shutdown];
- [self.persistence shutdown];
- if (completion) {
- [self.userDispatchQueue dispatchAsync:^{
- completion(nil);
- }];
- }
- }];
- }
- - (FSTQueryListener *)listenToQuery:(FSTQuery *)query
- options:(FSTListenOptions *)options
- viewSnapshotHandler:(FSTViewSnapshotHandler)viewSnapshotHandler {
- FSTQueryListener *listener = [[FSTQueryListener alloc] initWithQuery:query
- options:options
- viewSnapshotHandler:viewSnapshotHandler];
- [self.workerDispatchQueue dispatchAsync:^{
- [self.eventManager addListener:listener];
- }];
- return listener;
- }
- - (void)removeListener:(FSTQueryListener *)listener {
- [self.workerDispatchQueue dispatchAsync:^{
- [self.eventManager removeListener:listener];
- }];
- }
- - (void)writeMutations:(NSArray<FSTMutation *> *)mutations
- completion:(nullable FSTVoidErrorBlock)completion {
- [self.workerDispatchQueue dispatchAsync:^{
- if (mutations.count == 0) {
- [self.userDispatchQueue dispatchAsync:^{
- completion(nil);
- }];
- } else {
- [self.syncEngine writeMutations:mutations
- completion:^(NSError *error) {
- // Dispatch the result back onto the user dispatch queue.
- if (completion) {
- [self.userDispatchQueue dispatchAsync:^{
- completion(error);
- }];
- }
- }];
- }
- }];
- };
- - (void)transactionWithRetries:(int)retries
- updateBlock:(FSTTransactionBlock)updateBlock
- completion:(FSTVoidIDErrorBlock)completion {
- [self.workerDispatchQueue dispatchAsync:^{
- [self.syncEngine transactionWithRetries:retries
- workerDispatchQueue:self.workerDispatchQueue
- updateBlock:updateBlock
- completion:^(id _Nullable result, NSError *_Nullable error) {
- // Dispatch the result back onto the user dispatch queue.
- if (completion) {
- [self.userDispatchQueue dispatchAsync:^{
- completion(result, error);
- }];
- }
- }];
- }];
- }
- - (FSTDatabaseID *)databaseID {
- return self.databaseInfo.databaseID;
- }
- @end
- NS_ASSUME_NONNULL_END
|